Chapter 9 - Package and Interface
Chapter 9 - Package and Interface
Chapter 9 - Package and Interface
Package and
Interface
Dr. Mohammad Rashedur Rahman
Defining a Package
A package is both a naming and a visibility control mechanism:
1) divides the name space into disjoint subsets It is possible to define classes
within a package that are not accessible by code outside the package.
2) controls the visibility of classes and their members It is possible to define
class members that are only exposed to other members of the same package.
Same-package classes may have an intimate knowledge of each other, but not
expose that knowledge to other packages
Creating a Package
• A package statement inserted as the first line of the source file:
package myPackage;
class MyClass1 { … }
class MyClass2 { … }
• means that all classes in this file belong to the myPackage package.
• The package statement creates a name space where such classes are stored.
• When the package statement is omitted, class names are put into the default
package which has no name.
Multiple Source Files
• Other files may include the same package instruction:
1. package myPackage;
class MyClass1 { … }
class MyClass2 { … }
2. package myPackage;
class MyClass3{ … }
• A package may be distributed through several source files
Packages and Directories
• Java uses file system directories to store packages.
• Consider the Java source file:
package myPackage;
class MyClass1 { … }
class MyClass2 { … }
• The byte code files MyClass1.class and MyClass2.class must be stored in a
directory myPackage.
• Case is significant! Directory names must match package names exactly.
Package Hierarchy
• To create a package hierarchy, separate each package name with a
dot:
package myPackage1.myPackage2.myPackage3;
• A package hierarchy must be stored accordingly in the file system:
1) Unix myPackage1/myPackage2/myPackage3
2) Windows myPackage1\myPackage2\myPackage3
3) Macintosh myPackage1:myPackage2:myPackage3
• You cannot rename a package without renaming its directory!
Accessing a Package
• As packages are stored in directories, how does the Java run-time system
know where to look for packages?
• Two ways:
1) The current directory is the default start point - if packages are stored in
the current directory or sub-directories, they will be found.
2) Specify a directory path or paths by setting the CLASSPATH environment
variable.
CLASSPATH Variable
• CLASSPATH - environment variable that points to the root directory of the
system’s package hierarchy.
• Several root directories may be specified in CLASSPATH,
• e.g. the current directory and the C:\User\myJava directory:
.;C:\User\myJava
• Java will search for the required packages by looking up subsequent directories
described in the CLASSPATH variable.
Finding Packages
• The import statement occurs immediately after the package statement and before the
class statement:
package myPackage;
• import otherPackage1;otherPackage2.otherClass;
class myClass { … }
• The Java system accepts this import statement by default:
import java.lang.*;
• This package includes the basic language functions. Without such functions, Java is of no
much use.
Example: Packages 1
The importing code has access to the public class Balance of the MyPack
package and its two public members:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show();
}
}
Java Source File
• Finally, a Java source file consists of:
1) a single package instruction (optional)
2) several import statements (optional)
3) a single public class declaration (required)
4) several classes private to the package (optional)
• At the minimum, a file contains a single public class declaration.
Differences between classes and interfaces
• Interfaces are syntactically similar to classes, but they lack instance variables,
and their methods are declared without any body.
• One class can implement any number of interfaces.
• Interfaces are designed to support dynamic method resolution at run time.
More about interfaces
• Interface is little bit like a class... but interface is lack in instance variables....that's u can't
create object for it.....
• Interfaces are developed to support multiple inheritance...
• The methods present in interfaces are pure abstract..
• The access specifiers public,private,protected are possible with classes, but the interface
uses only one spcifier public.....
• interfaces contains only the method declarations.... no definitions.......
• A interface defines, which method a class has to implement. This is way - if you want to call
a method defined by an interface - you don't need to know the exact class type of an
object, you only need to know that it implements a specific interface.
• Another important point about interfaces is that a class can implement multiple interfaces.
Defining an interface
• Using interface, we specify what a class must do, but not how it does this.
• An interface is syntactically similar to a class, but it lacks instance
variables and its methods are declared without any body.
• An interface is defined with an interface keyword.
Defining an Interface
An interface declaration consists of modifiers, the keyword interface, the interface name, a comma-
separated list of parent interfaces (if any), and the interface body. For example:
public interface GroupedInterface extends Interface1, Interface2, Interface3 {
// constant declarations double E = 2.718282;
// base of natural logarithms //
//method signatures
void doSomething (int i, double x);
int doSomethingElse(String s);
}
The public access specifier indicates that the interface can be used by any class in any package. If you do not
specify that the interface is public, your interface will be accessible only to classes defined in the same package as
the interface.
An interface can extend other interfaces, just as a class can extend or subclass another class. However, whereas a
class can extend only one other class, an interface can extend any number of interfaces. The interface declaration
includes a comma-separated list of all the interfaces that it extends
Implementing interface
• General format:
access interface name {
type method-name1(parameter-list);
type method-name2(parameter-list);
…
type var-name1 = value1;
type var-nameM = valueM;
…
}
Implementing interface (cont..)
• Two types of access:
1) public – interface may be used anywhere in a program
2) default – interface may be used in the current package only
• Interface methods have no bodies – they end with the semicolon after
the parameter list.
• They are essentially abstract methods.
• An interface may include variables, but they must be final, static and
initialized with a constant value.
• In a public interface, all members are implicitly public.
Interface Implementation
• A class implements an interface if it provides a complete set of methods
defined by this interface.
1) any number of classes may implement an interface
2) one class may implement any number of interfaces
• Each class is free to determine the details of its implementation.
• Implementation relation is written with the implements keyword.
Implementation Format
B extends A:
interface B extends A {
void meth3();
}
Example: Interface Inheritance 2
• MyClass must implement all of A and B methods:
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}}
Example: Interface Inheritance 3
• Create a new MyClass object, then invoke all interface
methods on it:
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}