Lecture 18
Lecture 18
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
java.lang
Contains classes for primitive types, strings, math functions, threads, and
The benefits of organising classes into packages are: exception
java.util
programs/applications can be reused.
java.io
other packages. That two classes in two different packages can
java.awt
can be accessed with their fully qualified name.
java.net
packages to access them.
java.applet
Classes for creating and implementing applets
coding.
3 4
java.lang.Math.sqrt(x);
required for implementing GUI (graphical user
import java.lang.Math
interface).
Math.sqrt(x);
java
“java” Package containing
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
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
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.
import myPackage.ClassA;
{
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
15 16
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
17 18
package pack1;
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
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
23 24