Why do we need PACKAGES in java?

BOXIn our previous set of tutorials, you must have noticed the use of package and you have seen the import statements following the package statement in most of the classes. Yes, by now I expect you to have been familiar with these two keywords since I have briefly discussed them here but you probably don’t understand why we needed the packages, created the different packages in my last series of posts, put some classes within them and why I had to make call to the Import statement. Whatever the case, this post is to clear your doubts, if any, and make you understand why packages are important.

First, when you think about a package, what comes to your mind? For me, I think of a wrapped box with related Items – remember when you are checking out at the mall, the attendant packs related items together. For example, food items are not put close to detergents. Packages in Java does something similar to that of the mall attendant. It brings together related classes and interface into the same package so as to avoid mix up. For the sake of definition, a package is a collection of related classes and interfaces. To have a clearer view, let us assume families A, B, C and D with two children each with the profession shown in the illustration below:

Continue reading