“Packages in Java”
Contents Introduction Classification Java API packages Using system packages Naming conventions Creating package Accessing package Using a package Conclusion Bibliography
Introduction Packages are Java’s way of grouping a variety of classes and/or interfaces together
Classification Java packages are classified into two types 1) Java API 2) User defined packages
Java API package JAVA lang util io awt net applet
Using system package Java awt Package containing awt package Color Graphics Package containing classes Font Classes containing methods Image
Naming convention double y = java . lang . Math . sqrt (x); Package name Class name Method name
Creating package package firstPackage; public class FirstClass { ……………………… } (Body of class)
Continued…. Declare the package at the beginning of a file using the form Define the class that is to be put it the package and declare it public Create a sub directory under the directory where the main source files are stored package packagename;
Continued…… Store the listing as the classname .java file in the sub directory created Compile the file. This creates .class file in the sub directory
Accessing a Package import package1 [.package2] [.package3] .classname;
Using a Package package package1; public class ClassA { public void displayA() System.out.println(“welcome”); s } }
Cont.. import package1.classA; class Test { public static void main(String args[]) classA obj=new classA(); obj.displayA(); }
conclusion Ability to reuse the code within the same programs is achieved in oops by means of extending and implementing. Including this property java also provides a mechanism,that is reuse of the code out side the program without physical copy, is achieved by using packages.