Introduction
The main feature of OOP is its ability to support the
reuse of code:
Packages: Putting Classes
Extending the classes (via inheritance)
Extending interfaces
Together 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.
1 2
Packages Java Foundation Packages
Packages are Java’s way of grouping a number of
Java provides a large number of classes groped into different
related classes and/or interfaces together into a single packages based on their functionality.
unit. That means, packages act as “containers” for
The six foundation Java packages are:
classes.
java.lang
Contains classes for primitive types, strings, math functions, threads, and
The benefits of organising classes into packages are: exception
The classes contained in the packages of other
java.util
programs/applications can be reused.
Contains classes such as vectors, hash tables, date etc.
In packages classes can be unique compared with classes in
java.io
other packages. That two classes in two different packages can
Stream classes for I/ O
have the same name. I f there is a naming clash, then classes
java.awt
can be accessed with their fully qualified name.
Classes for implementing GUI – windows, buttons, menus etc.
Classes in packages can be hidden if we don’t want other
java.net
packages to access them.
Classes for networking
Packages also provide a way for separating “design” from
java.applet
Classes for creating and implementing applets
coding.
3 4
Using System Packages Accessing Classes from Packages
The packages are organised in a hierarchical structure.
There are two ways of accessing the classes stored in
packages:
For example, a package named “java” contains the
Using fully qualified class name
package “awt”, which in turn contains various classes
java.lang.Math.sqrt(x);
required for implementing GUI (graphical user
Import package and use class name directly.
import java.lang.Math
interface).
Math.sqrt(x);
java
“java” Package containing
Selected or all classes in packages can be imported:
lang
“lang”, “awt”,.. packages;
Can also contain classes. import package.class;
awt import package.*;
Graphics awt Package containing
Implicit in all programs: import java.lang.* ;
classes
Font
package statement(s) must appear first
Classes containing
Image
methods
…
5 6
Creating Packages Creating Sub Packages
Java supports a keyword called “package” for creating Classes in one ore more source files can be part of the
user-defined packages. The package statement must same packages.
be the first statement in a Java source file (except
comments and white spaces) followed by one or more
As packages in Java are organised
classes. hierarchically, sub-packages can be created as
package myPackage;
public class ClassA { follows:
/ / class body
package myPackage.Math
}
package myPackage.secondPakage.thirdPackage
class ClassB {
/ / class body Store “thirdPackage” in a subdirectory named
} “myPackage\secondPackage”. Store “secondPackage”
Package name is “myPackage” and classes are and “Math” class in a subdirectory “myPackage”.
considred as part of this package; The code is saved in
a file called “ClassA.java” and located in a directory
called “myPackage”. 7 8
Accessing a Package Using a Package
As indicated earlier, classes in packages can be Let us store the code listing below in a file named
accessed using a fully qualified name or using a “ClassA.java” within subdirectory named “myPackage”
short-cut as long as we import a corresponding within the current directory (say “abc”).
package.
The general form of importing package is: package myPackage;
import package1[.package2][ …].classname public class ClassA {
Example: / / class body
public void display()
import myPackage.ClassA;
{
import myPackage.secondPackage System.out.println("Hello, I am ClassA");
All classes/ packages from higher-level package can }
be imported as follows: }
import myPackage.* ; class ClassB {
/ / class body
}
9 10
Using a Package Compiling and Running
Within the current directory (“abc”) store
When ClassX.java is compiled, the compiler
compiles it and places .class file in current
the following code in a file named directly. If .class of ClassA in subdirectory
“ClassX.java” “myPackage” is not found, it comples ClassA
import myPackage.ClassA; also.
public class ClassX
Note: It does not include code of ClassA into
{ ClassX
public static void main(String args[ ] )
{
When the program ClassX is run, java loader
ClassA objA = new ClassA(); looks for ClassA.class file in a package called
objA.display();
}
“myPackage” and loads it.
}
11 12
Using a Package Using a Package
Let us store the code listing below in a file named Within the current directory (“abc”) store
“ClassA.java” within subdirectory named
“secondPackage” within the current directory (say the following code in a file named
“abc”). “ClassX.java”
package secondPackage; import myPackage.ClassA;
public class ClassC { import secondPackage.ClassC;
/ / class body public class ClassY
public void display() {
{ public static void main(String args[ ] )
System.out.println("Hello, I am ClassC"); {
} ClassA objA = new ClassA();
} ClassC objC = new ClassC();
objA.display();
objC.display();
}
13 } 14
Output Protection and Packages
[ raj@mundroo] package % java ClassY
All classes (or interfaces) accessible to all
others in the same package.
Class declared public in one package is
Hello, I am ClassA
accessible within another. Non-public class is
Hello, I am ClassC not
Members of a class are accessible from a
[ raj@mundroo] package % 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 16
Visibility - Revisited Visibility Modifiers
Accessible to: public protected Package private
Public keyword applied to a class, makes it (default)
available/ visible everywhere. Applied to a Same Class Yes Yes Yes Yes
method or variable, completely visible.
Private fields or methods for a class only visible Class in package Yes Yes Yes No
within that class. Private members are not
Subclass in Yes Yes No No
visible within subclasses, and are not inherited. different package
Protected members of a class are visible within Non-subclass Yes No No No
the class, subclasses and also within all classes different package
that are in the same package as that class.
17 18
package pack1;
Adding a Class to a Package Adding a Class to a Package class Teacher
class Student
Consider an existing package that contains a Define the public class “Student” and place the package
statement before the class definition as follows:
class called “Teacher”:
package pack1;
package pack1; public class Student
public class Teacher {
{ / / class body
/ / class body }
}
This class is stored in “Teacher.java” file within Store this in “Student.java” file under the directory
“pack1”.
a directory called “pack1”. When the “Student.java” file is compiled, the class file
How do we a new public class called “Student” will be created and stored in the directory “pack1”.
Now, the package “pack1” will contain both the classes
to this package. “Teacher” and “Student”.
19 20
Packages and Name Clashing Handling Name Clashing
When packages are developed by different
In Java, name classing is resolved by accessing
organizations, it is possible that multiple packages will classes with the same name in multiple
have classes with the same name, leading to name
classing. package pack1;
packages by their fully qualified name.
package pack2;
Example:
class Teacher class Student import pack1.* ;
import pack2.* ;
class Student class Courses
pack1.Student student1;
We can import and use these packages like: pack2.Student student2;
import pack1.* ; Teacher teacher1;
import pack2.* ; Courses course1;
Student student1; // Generates compilation error
21 22
Extending a Class from Package Summary
A new class called “Professor” can be Packages allow grouping of related
created by extending the “Teacher” class classes into a single united.
defined the package “pack1” as follows: Packages are organised in hierarchical
import pack1.Teacher;
structure.
public class Professor extends Teacher
{
Packages handle name classing issues.
/ / body of Professor class
/ / It is able to inherit public and protected members, Packages can be accessed or inherited
}
/ / but not private or default members of Teacher class. without actual copy of code to each
program.
23 24