You are on page 1of 1

Nested Packages

------------------
A nested package is a package within
a package.
It is all similar to a package, only
difference is that it is nested indside
another package.
Why Nested Packages?
----------------------
A nested package allows better organization of classes and interfaces.
Case 1: Without nested package
Vehicle {Bike, Car, Truck}
Case 2: With nested package
Vehicle { TwoWheeler {Bike}, FourWheeler{Car, Truck} }
To create a nested package
-------------------------
Create a sub directory inside the
main package directory.
The sub directory must have the
same name as of the nested package.
To put classes and interfaces in a nested package
-------------------------------------------
1) Define the classes and interfaces with "nested package statement" as its firs
t statement.
2) Place the classes and interfaces in the sub directory of the nested package.
How to use a nested package?
--------------------------
The "public" classes and interfaces of
a nested package are "imported" and
used by other classes and interfaces.
How to use import for nested packages?
------------------------------------
import statement for a nested package is as follows:
import pckgName.nestedPckgName.className;
import pckgName.nestedPckgName.*;

You might also like