Packages: Putting Classes Together
Packages: Putting Classes Together
Packages: Putting Classes Together
Introduction
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.
2
Packages
Packages are Javas way of grouping a number of related classes and/or interfaces together into a single unit. That means, packages act as containers for classes. 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 dont want other packages to access them. Packages also provide a way for separating design from coding.
3
Java provides a large number of classes groped into different packages based on their functionality. The six foundation Java packages are:
java.lang
java.util
Contains classes for primitive types, strings, math functions, threads, and exception
java.io
java.awt
java.net
java.applet
The packages are organised in a hierarchical structure. For example, a package named java contains the package awt, which in turn contains various classes required for implementing GUI (graphical user interface).
java
lang awt Graphics Font Image java Package containing lang, awt,.. packages; Can also contain classes.
Implicit in all programs: import java.lang.*; package statement(s) must appear first
Creating Packages
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.
package myPackage; public class ClassA { // class body } class ClassB { // class body }
Package name is myPackage and classes are considred as part of this package; The code is saved in a file called ClassA.java and located in a directory 7 called myPackage.
Classes in one ore more source files can be part of the same packages.
Store thirdPackage in a subdirectory named myPackage\secondPackage. Store secondPackage and Math class in a subdirectory myPackage.
Accessing a Package
As indicated earlier, classes in packages can be accessed using a fully qualified name or using a short-cut as long as we import a corresponding package. The general form of importing package is:
import myPackage.*;
Using a Package
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
10
Using a Package
Within the current directory (abc) store the following code in a file named ClassX.java
import myPackage.ClassA; public class ClassX { public static void main(String args[]) { ClassA objA = new ClassA(); objA.display(); } }
11
When ClassX.java is compiled, the compiler compiles it and places .class file in current directly. If .class of ClassA in subdirectory myPackage is not found, it comples ClassA also. Note: It does not include code of ClassA into ClassX When the program ClassX is run, java loader looks for ClassA.class file in a package called myPackage and loads it.
12
Using a Package
Let us store the code listing below in a file named ClassA.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"); } }
13
Using a Package
Within the current directory (abc) store the following code in a file named ClassX.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(); } }
14
All classes (or interfaces) accessible to all others in the same package. Class declared public in one package is accessible within another. Non-public class is not Members of a class are accessible from a difference class, as long as they are not private protected members of a class in a package are accessible to subclasses in a different class
15
Visibility - Revisited
available/visible everywhere. Applied to a method or variable, completely visible. Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class.
16
Visibility Modifiers
Accessible to: public protected Package (default) private
Same Class
Yes Yes No No
Yes No No No
Class in package
17
This class is stored in Teacher.java file within a directory called pack1. How do we add a new public class called Student to this package.
18
package pack1;
Define the public class Student and place the package statement before the class definition as follows:
package pack1; public class Student { // class body }
Store this in Student.java file under the directory pack1. When the Student.java file is compiled, the class file will be created and stored in the directory pack1. Now, the package pack1 will contain both the classes Teacher and Student.
19
When packages are developed by different organizations, it is possible that multiple packages will have classes with the same name, leading to name classing. package pack1; package pack2;
class Teacher
class Student class Student class Courses
In Java, name classing is resolved by accessing classes with the same name in multiple packages by their fully qualified name. Example:
import pack1.*; import pack2.*; pack1.Student student1; pack2.Student student2; Teacher teacher1; Courses course1;
21
A new class called Professor can be created by extending the Teacher class defined the package pack1 as follows:
import pack1.Teacher; public class Professor extends Teacher { // body of Professor class // It is able to inherit public and protected members, // but not private or default members of Teacher class. }
22
Summary
Packages allow grouping of related classes into a single united. Packages are organised in hierarchical structure. Packages handle name classing issues. Packages can be accessed or inherited without actual copy of code to each program.
23
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: