Remember the Illustration of the four families as shown in the picture above? Click here to refresh you memory. I explained that family “A” member can call members of families B, C and D and vice versa. Today, I will be explaining how this could be achieved, I promised three things in my last post on package: 1.)How to make a class callable outside its package, 2.)The import statement and 3.) The different styles of import. Hence, to fulfill my promise I will be providing answers to these key points today.
In 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 Importstatement. 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:
Recently, one of my BCB readers asked me a question that brought about this blog topic, he said “I have read through your several posts but how do I bring them all together and write a complete functional code?” Then I thought to myself that he was right, probably other readers might also have the same challenge bringing all the pieces together. Therefore, in this post and my next couple of posts, we will be taking a deep dive, referring back at every topic on Java we have previously dealt with on this blog in order to write a complete functional code. I hope you are ready to get your hands really dirty…lol.
Assumption: To start with, I assume that by now you have installed Java and you have java path set properly on your machine. If not, please refer to my previous post on this Lay your Bed this Christmas. Remember to direct all questions to me should you have any challenge following the steps. I will be using Netbeans IDE (Integrated Development Environment) for this example, you can use any IDE you are comfortable with to run this example. See list of possible IDEs and where to download them on this post Lay your Bed this Christmas.
Task: We will be writing a sample program to calculate the area of different basic shapes i.e. rectangle, square, circle etc..