Friday, August 23, 2013

PACKAGES IN JAVA

WHAT ARE PACKAGES IN JAVA ?

When making any project in java, the project may constitute large number of classes and interfaces which are assigned to different development teams to code them. They place classes and interfaces having same category into packages. Packages are nothing but named directory /folder, where programmers keep classes and interfaces of same functionality.
e.g.
Suppose, you've made different classes such as :
Lion
Tiger
Peacock
Vulture
Car
Bus
We can easily sense some kind of similarities among certain classes, such as:
Lion and Tiger are ANIMALS
Peacock and Vulture are BIRDS 
Car and Bus are VEHICLES
Instead of keeping all these classes together, we would surely like to categorize classes of similar types together for well organization.
Therefore, we would put classes Lion and Tiger in ANIMAL PACKAGE.
Peacock and Vulture in BIRDS PACKAGE
Car and Bus in VEHICLES PACKAGE


HOW DO WE CREATE PACKAGE IN JAVA ?

When we create a class, we start by writing e.g.

public class <name_of_class> {
Data Members
/ / / / / / / / / / 
/ / / / / / / / / 
Methods
/ / / / / / / / / 
 / / / / / / / 
}

So, we want the above class to belong to some package, we just have to write the 'package' keyword with a appropriate name given to that package as the first line of the program.

e.g.

package  <name_of_package>;
public class <name_of_class> {
Data Members
/ / / / / / / / / / 
/ / / / / / / / / 
Methods
/ / / / / / / / / 
 / / / / / / / 
}

NOTE : if we want our class to belong to some package, then we must begin our program with the package keyword and the name of the package as 1st line of the program.
By doing this, a directory / folder gets created of the same name as that of package, in which our class get stored.

WHAT ARE SUB-PACKAGES IN JAVA ?

As name itself indicates, it is just another package inside the package.Lets take a scenario to understand the concept of sub-packages. Suppose we created a package called ANIMALS
and we have made classes such as :
Lion
Tiger
Peacock
Eagle
Salmon
Shark
It is absolutely clear that all the above classes are animals, but, if we look further, then we can sense that:
Lion and Tiger are NON-FLYING ANIMALS
Peacock and Eagle are FLYING ANIMALS
Salmon and Shark are SEA ANIMALS
Therefore, there arises a need to categorize them further. In-order to deal with this situation we need to put them in packages further, and all these newly formed packages will go into ANIMAL PACKAGE.

Now, ANIMAL PACKAGE Contains 3 SUB-PACKAGES namely :
NON-FLYING ANIMALS ( containing classes : Lion and Tiger )
FLYING ANIMALS ( containing classes : Peacock and Eagle )
SEA ANIMALS ( containing classes : Salmon and Shark )

HOW DO WE CREATE SUB-PACKAGES IN JAVA ?

Suppose we have classes Tiger and Eagle. We know that both of these are animals, but at the same time both belong to a different category. Tiger belongs to Non flying category and Eagle belongs to Flying category. So, we need to put these classes into different sub-packages, and to do that, we begin the class code with 'package' keyword then name of the package then period(a dot) then the name of the sub-package. 
e.g.

package animals.non_flying_animals;
public class Tiger {
/ / / / /
 / / / 
}
By doing so, the class Tiger will get stored in directory / folder structure:  animals / flying_animals

To put Eagle class in another sub-package, we would insert tha package statement as :

package animals.flying_animals;
public class Eagle {
/ / / / /
 / / / 
}

By doing so, the class Eagle will get stored in directory / folder structure as :
animals / flying_animals

NOTE : i) the name of the package should start with a small case.
ii) the name must not contain blank spaces in between as in 'non flying animals', rather it should be written as 'non_flying_animals'.

No comments:

Post a Comment