Page 456 - Cs_withBlue_J_C11_Flipbook
P. 456
15.3 IMPORTING USER-DEFINED PACKAGES
The import keyword is used to make the classes and interface of another package accessible to the current package.
Let us demonstrate it with an example:
Step 1: Create a class (Say Cylinder) in another package. The program is as shown below:
Statement import Eleven.* is to used to import public class Area under package Eleven. To execute method
getdata() and calArea() in class Area, an object obj is created of class type Area. The concerned methods are
called using objects. The value returned by calArea() method is used in the calculation of the volume of the
cylinder.
Step 2: After compilation, Java gives the message ‘Class compiled - no syntax errors’. Then close the class window.
Step 3: Right click on the class icon to create an object and execute the calVolume() method as shown.
Step 4: The result will be displayed. The method calVolume() used result of calArea() belonging to class Area on a
separate package Eleven.
The following points are to be kept in mind while writing package programming in Java:
• Every class belongs to a package. If we omit the package statement, the class names are put into the default package.
• A class can have only one package statement but it can have multiple import package statements.
• The name of the package must be similar to the directory under which the file is stored.
• When importing any package, package declaration must be the first statement.
454454 Touchpad Computer Science-XI

