Google+

213. Creating Hierarchy of Packages








We can create packages at different hierarchy level (package and its sub-package etc). Lets create the packages at different hierarchy levels as shown below -



Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE and create a Java Project 'Project 41' as shown below -


2. Now lets change the display mode of the package hierarchy by following the below steps.

3. Select the 'view' menu icon of the 'Package Explorer' tab as shown  below -


4. In 'View Menu' options change the Package Presentation option from Flat to Hierarchical as shown below -


Now with this step, the package display mode got changed to hierarchical mode. Lets create hierarchy of packages by following the below steps.

5. Create two packages 'package1' and 'package2' as shown below -


Now lets create the packages 'packageA' and 'packageB' as sub-packages under 'package2' by following the below steps.

6. Now right click on the 'package2' and select 'New' -> 'Package' from the right click options as shown below -



7. In New Package dialog, enter the package name as package2.packageA and click on 'Finish' button  as shown below -


8. Observe that the package 'package2' got changed to 'package2.packageA' as shown below -


When you create a single sub-package, the original package will get replaced with a single package 'package.subpackage'. But when you create more than one sub-packages for a package, you can clearly see the hierarchy. Please follow the below steps to create another sub-package for 'package2'.

9. Right click on the 'package2.packageA' package and select 'New' -> 'Package' from the right click options as shown below -



10. Ensure that the 'New Java Package' dialog is displayed with 'package2.packageA' in the name field as by default as shown below -


11. Change the packageA to packageB in the default displayed name and click on 'Finish' button as shown below -


12. Observe that the two sub-packages 'packageA'  and 'packageB' are displayed under the 'package2' package as shown below -


This is how we create packages at different hierarchy levels and view them in the Hierarchical package display mode.

Now lets find out the package statements created in the Class files which are created under the package which is not having any sub-pakcage by following the below steps.

13. Create a Class 'ClassOne' under 'package1' page and observe that the package statement is displayed in the Class file as shown below -


Hence when you create a Class file under a package directly, the package statement syntax will be package packageName; 

Now lets create a Class file under a sub-package 'packgeB' of 'package2' package by following the below steps.

14. Create a Class 'ClassX' under 'packageB' of 'package2' and observe that the package statement is displayed in the Class file as shown below -


Hence when you create a Class file under a sub-package of any package, the package statement syntax will be package packageName.subPackageName; 

Now lets change the package view from Hierarchical mode, back to the Flat mode by following the below steps.

15. Select 'Flat' option from the 'View Menu' options as shown below -



16. Observe that the hierarchical mode got changed to the Flat mode as shown below -



When you create a package in Eclipse IDE, a folder will be created in the workspace in your computer (i.e. Inside Eclipse IDE Workspace -> Project folder). Now lets view the workspace of this project to verify the same by following the below steps.

17. Right Click on the Project, select 'Properties' option and view the location of your project on your computer as shown below -


18. Navigate to the location on your computer and view the Project files as shown below -



19. Open the 'src' folder and observe that the Packages 'package1' and 'package2' are displayed as folder as shown below -


20. Open the 'package2' folder and observe that 'packageA' and 'packageB' sub folder for the sup-packages in Eclipse IDE are displayed as shown below -


21. Open the 'packageB' sub-folder and observe that ClassX created under the sub-package 'packageB' is displayed as shown below -








Please comment below to feedback or ask questions.

Understanding the packages in Java API will be explained in the next post.






No comments:

Blog Index