Packages & Interfaces
Definition: A package is a collection of classes, interfaces and other packages.
Packages resemble directories or folders.
Packages are used to group the logical related classes and interfaces.
Packages help in avoiding name conflicts.
Creation of package: Creation of a package is simply putting a class or interface in
it. So for this we need to add a package statement in the class or the interface we define.
package packageName;
Ex: 1
package graphics;
class Circle{
…….
}
package graphics;
class Rectangle{
…….
}
A package statement should be written at the top of every source/ java file.
If a class doesn’t have the package statement, then the class is placed in the default
package, which is the current directory location.
Accessing a package: For example if we have created the above package graphics in
say d:\ java\ graphics\[Link]
Then if we run the [Link] program. It will first search for the class in the current
directory or in the location where the CLASSPATH environmental variable is set.
If we always run any java program from one level above the package level, then there is
no need to set the classpath variable.
But if we set the CLASSPATH variable, then we have to run the java programs from that
path.
Importing packages:
package graphics; package graphics3D ;
[Link] extends [Link]
[Link]
[Link]
If a class Rectangle3D extends a class Rectangle existing in some other package, it has to
first import that package into the current package in order to use the classes in it.
Ex:
package graphics3D;
import graphics;
class Rectangle3D extends Rectangle{
…..
}
import is the keyword used to access the classes or interfaces of one package into
another.
Interface: An interface is a way to declare a type consisting only of abstract methods
and related constants (final data), enabling any implementation to be written for those
methods. An interface provides a protocol binding to the classes that implement it.
An interface is a collection of abstract methods and constants (final data members).
An interface cannot be instantiated.
An interface can contain only final type variables.
Fields in an interface are always static and final.
interface is the keyword used for creating interfaces.
accessSpecifier interface name{
type final-variable1 = value;
type final-variable2 = value;
… ..
.
return-type method-name1(parameter list);
return-type method-name2(parameter list);
..
..
}
Ex 1:
interface Polygon{
float PI = 3.13f;
float area();
float circumference();
}
All the methods in an interface are implicitly abstract.
The abstract methods of an interface are implemented by a class using implements
keyword.
Implementing an Interface:
Ex 1 continued.
class Circle implements Polygon{
public float radius:
public Circle( float r){
radius= r;
}
public float area(){
return PI * radius * radius ;
}
public float circumference{
return 2*PI * radius ;
}
}
class PolygonUse{
public static void main(String args[]){
Circlew c= new Circle();
System..[Link](“Area of circle = ”+ [Link]() );
System..[Link](“Circumference of circle = ”+ [Link]() );
}
The class which implements an interface should give definitions to all its abstract
methods.
If the above Polygon class doesn’t give the implementation to all the abstract methods of
the interface, then the class has to be declared as abstract.
Differences between classes and interfaces:
Classes Interfaces
1) Classes can be instantiated 1) Interfaces cannot be instantiated
2) Classes can have all types of data 2) Interfaces can have only final variables.
members.
3) One class can extend the function of 3) A class implements an interface using
another class using the extends keyword. the implements keyword.
4) Methods in classes are concrete/ 4) Methods in an interface are by default
abstract abstract
5) Classes are defined with keyword 5) Interfaces are defined using the keyword
“class”. “interface”
Extending interfaces:
An interface can extend another interface. Unlike classes, an interface can extend more
than one interface.
Ex:
interface A{
…..
}
interface B extends A{
…..
}
class C implements B{
……
}
Note: Multiple inheritance is not directly supported in java through classes, but can be
achieved using interfaces.