Page 407 - CITS - Computer Software Application -TT
P. 407

COMPUTER SOFTWARE APPLICATION - CITS

           Packages in JAVA Creating and Using

           In this tutorial, we are going to discuss packages in Java with the help of example programs. In small projects, all
           the Java files have unique names. So, it is not difficult to put them in a single folder.
           But, in the case of huge projects where the number of Java files is large, it is very difficult to put files in a single
           folder because the manner of storing files would be disorganized.
           Moreover, if different Java files in various modules of the project have the same name, it is not possible to store
           two Java files with the same name in the same folder because it may occur naming conflict.

           This problem of naming conflict can be overcome by using the concept of packages. In Java, APIs consist of one
           or more packages where packages consist of many classes, classes contain several methods and fields.
           When you create an application  in Java, you should  create a proper folder structure for better reusability,
           maintenance, and avoiding naming conflict but How?
           What is Package in Java
           A package is nothing but a physical folder structure (directory) that contains a group of related classes, interfaces,
           and sub-packages according to their functionality. It provides a convenient way to organize your work. The Java
           language has various in-built packages.
           For  example,  java.lang,  java.util,  java.io,  and  java.net. All  these  packages  are  defined  as  a  very  clear  and
           systematic packaging mechanism for categorizing and managing.
           Let’s understand it with the help of real-time examples.
           Realtime Example of Packages in Java
           A real-life example is when you download a movie, song, or game, you make a different folder for each category
           like movie, song, etc. In the same way, a group of packages in java is just like a library.
           The classes and interfaces of a package are like books in the library that can reuse several times when we need
           them. This reusability nature of packages makes programming easy.
           Therefore, when you create any software or application in Java programming language, they contain hundreds or
           thousands of individual classes and interfaces.

           Advantage of using Packages in Java
           1   Maintenance: Java packages are used for proper maintenance. If any developer newly joined a company, he
              can easily reach to files needed.

           2   Reusability: We can place the common code in a common folder so that everybody can check that folder and
              use it whenever needed.
           3   Name conflict: Packages help to resolve the naming conflict between the two classes with the same name.
              Assume that there are two classes with the same name Student.java. Each class will be stored in its own
              packages such as stdPack1 and stdPack2 without having any conflict of names.
           4   Organized: It also helps in organizing the files within our project.

           5   Access Protection: A package provides access protection. It can be used to provide visibility control. The
              members of the class can be defined in such a manner that they will be visible only to elements of that
              package.
           Types of Packages in Java
           There are mainly two types of packages available in Java. They are:
           •  User-defined package
           •  built-in package (also called predefined package)

           Let’s understand first user-defined package and how to create it easily in a Java program.
           User-defined Package in Java
           The package which is defined by the user is called user-defined or custom package in Java. It contains user-
           defined classes and interfaces. Let’s understand how to create a user-defined package.




                                                           394

                             CITS : IT&ITES - Computer Software Application - Lesson 109 - 115
   402   403   404   405   406   407   408   409   410   411   412