Java :- Packages

Why we need packages in Java

  1. To bundle classes and interfaces we use packages in Java.
  2. To arrange the classes and interface in order so that we can easily find them.
  3. To void naming convention conflicts.
  4. To provide the control access
Definition :- A package is a grouping of related classes and interface providing access protection and name space management

Creating packages in Java

To create a package, you choose a name for the package and put a package statement with that name at the top of every source file.

Example :-
package  <package name>;

Note:- 

  1. There can be only one package statement in each source file.
  2. If you do not use a package statement, your type ends up in an unnamed package. Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.
  3. For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).
  4. If you import a package, subpackages will not be imported.
Naming Conventions

  1. Package names are written in all lower case to avoid conflict with the names of classes or interfaces
  2. Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.
  3. Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).
Using Package Members

To use a public package member from outside its package, you must do one of the following:
  1. Refer to the member by its fully qualified name
  2. Import the package member
  3. Import the member's entire package
Referring to a Package Member by Its Qualified Name

let see with the below example, how we can refer a package member by it qualified Name.

we have a class in package2 with name as child and we are trying to access the function of child class in our new class named as day5.





Importing a Package Member

Now let see the same example where we will be using child class by importing a package.

suppose we have more than one class present in package2, but we want to access only one class



Importing an Entire Package

now suppose we have lot of classes in a package2 and we want to use lot of functions from all these class then we generally import entire package instead of importing a single java file.

Now to import all the classes present in the packages we * after the package name.

Please have a look to below example.


NOTE:- Importing entire package doesn't means that we have imported the sub packages.

Access Protection of packages





Example for protected function in a class.







The Static Import Statement

There are situations where you need frequent access to static final fields (constants) and static methods from one or two classes. Prefixing the name of these classes over and over can result in cluttered code. The static import statement gives you a way to import the constants and static methods that you want to use so that you do not need to prefix the name of their class.

Let take a example to understand what static import is.



now let see how we can use this method in another class of present in different package.



Now let see how access specifier work for all the members present in class.

class members :- variables, methods.

Public :- If members in classes are public we can access those members outside the package in some other packages.

Protected :- If class members are protected then we can access those variable in same package and if want to access those members in another package then we need to establish parent child relationship.

Default :- default members can be accessed with in same packages.

Protected :- protected members can't be accessed outside a class.

Post a Comment