














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















The main feature of OOP is its ability to support the reuse of code: The features in basic form limited to reusing the classes within a program. What if we need to use classes from other programs without physically copying them into the program under development? In Java, this is achieved by using what is known as “packages”, a concept similar to “class libraries” in other languages.
The benefits of organising classes into packages are: The classes contained in the packages of other programs/applications can be reused. In packages classes can be unique compared with classes in other packages. That two classes in two different packages can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name. Classes in packages can be hidden if we don’t want other packages to access them. Packages also provide a way for separating “design” from coding.
Java provides a large number of classes grouped into different packages based on their functionality. The six foundation Java packages are: java.lang Contains classes for primitive types, strings, math functions, threads, and exception java.io Stream classes for I/O java.awt (abstract windowing kit) Classes for implementing GUI – windows, buttons, menus etc. java.net Classes for networking java.util Contains classes such as vectors, hash tables, date etc. java.applet Classes for creating and implementing applets
Using fully qualified class name java.lang.Math.sqrt(x); Import package and use class name directly. import java.lang.Math Math.sqrt(x);
Java supports a keyword called “package” for creating user-defined packages. The package statement must be the first statement in a Java source file (except comments and white spaces) followed by one or more classes. The file is saved as “ClassA.java” and code is located in directory “myPackage”. package myPackage; public class ClassA { // class body } class ClassB { // class body }
import package1[.package2][…].classname Example: import myPackage.ClassA; import myPackage.secondPackage All classes/packages from higher-level package can be imported as follows: import myPackage.*;
Let us store the code listing below in a file named “ClassA.java” within subdirectory named “myPackage” within the current directory (say “abc”). package myPackage; public class ClassA { // class body public void display() { System.out.println("Hello, I am ClassA"); } } class ClassB { // class body }
Let us store the code listing below in a file named “ClassC.java” within subdirectory named “secondPackage” within the current directory (say “abc”). package secondPackage; public class ClassC { // class body public void display() { System.out.println("Hello, I am ClassC"); } }
Within the current directory (“abc”) store the following code in a file named “ClassY.java” import myPackage.ClassA; import secondPackage.ClassC; public class ClassY { public static void main(String args[]) { ClassA objA = new ClassA(); ClassC objC = new ClassC(); objA.display(); objC.display(); } }
let's put the HelloWorld class along with its package (world) be under: C:\myclasses directory instead.The new location of our HelloWorld should be : set CLASSPATH=.; C:\myclasses; C:\world>java world.HelloWorld
package mypackage; public class myClass { … } // myClass is part of mypackage
package mypackage.mysubpackage;
Class access within a package Classes within a package can refer to each other without full qualification If a class, or member within a class, is not declared public, it can only be accessed by other classes within the package Class access across packages A public class can be accessed from other packages Its name is fully qualified or it must be imported to achieve this The public classes of a package can be seen as the interface of the package with the outside world Importing a package does not automatically import subpackages E.g. import java.awt.* does not import java.awt.font