Introduction
Other Object Oriented Programming languages, such as C++, provide multiple inheritance (deriving a single new class from two or more base classes). But such type of inheritance leads in ambiguity when two base classes have the methods with the same name. Fortunately Java does not support multiple inheritance. But it can not overlook the benefit of multiple inheritance. Therefore in order to enjoy the benefits of multiple inheritance, Java provides interfaces. An interface forces the classes to implement certain methods with certain signatures. Actually an interface takes the abstract concept one step ahead.
Interfaces
Interfaces are used to define a behavior of a class. That is, using interface you can specify what a class must do, but not how it does. Once an interface is defined you can implement any number of classes from this interface. Also one class can implement any number of interfaces.
Syntax
An interface is defined like a class, but using a keyword interface rather than class as:
public interface InterfaceName{
// ….}
Here the access specifier is used either public or not used. An interface contains constant values (final variables) and methods declarations. The difference between interface and classes is that the methods in an interface are only declared and not implemented, that is, the methods do not have a body. In other words there can be no default implementation of any method specified within an interface.
Example
public interface BankEmployee{
static final int count = 10;void setdata();
void display();}
Since the methods of an interface have to be overridden by the classes that implement it, therefore they can not be declared final. The data members can be public, static or final. Here public ensures that it can be accessed in unrelated classes; static ensures that you can not create an object of the interface and final ensures that the value is not changed in the classes that implement the interface.
Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. The keyword implements is used in order to implement an interface by a class.
Syntax
The keyword implements is used in order to implement an interface by a class.
class ClassName implements InterfaceName{
// ….}
Each class that includes an interface must implement all of the methods.
Example
class Manager implements BankEmployee{
// ….void setdata(){
// ….
}void display(){
// ….
}
}
Extending Interfaces
Like classes, interfaces can also be extended. It means that an interface can be subinterfaced from other interfaces. The new subinterface will inherit all the members of the super interface.
Syntax
A new interface is inherited from the old interface using the keywords – extends, as:
interface SubInterfaceName implements SuperInterfaceName{// ….
}
Here one should remember that when a class implements that inherits another interface, it must provide implementation for all the methods defined within the interface chain.
Program ExtendInterfaceDemo.java illustrates this concept.
// Program – ExtendInterfaceDemo.javainterface first{
void show1();
}interface second extends first{
void show2();}
class third implements second{public void show1(){ // Implementing first interface method declaration
System.out.println("First interface method");
}public void show2(){ // Implementing second interface method declaration
System.out.println("Second interface method");}
}class ExtendInterfaceDemo{
public static void main(String args[]){
third t = new third();
t.show1();t.show2();
}
}
The output of this program is….First interface method
Second interface method
Grouping Interfaces (Multiple Inheritance)
As stated earlier, Java does not directly supports multiple inheritance. But being a pure Object Oriented Programming Language, it can not forget the lovely taste of inheritance. Java enjoys the benefits of multiple inheritance by grouping several interfaces, which are separated by commas, into one interface.
Syntax
interface InterfaceName1{// ….
}interface InterfaceName2{
// ….}
interface InterfaceName3 extends InterfaceName1, InterfaceName2{// ….
}
Remember that the subinterfaces are too interfaces therefore you can not define the methods declared in super interfaces. When we implement a class from a subinterface then it is the duty of this class to define all the methods of superinterfaces as well as subinterfaces.
Program GroupInterfaceDemo.java illustrates this.
// Program – GroupInterfaceDemo.javainterface first{
void show1();}
interface second{void show2();
}interface third{
void show3();}
class forth implements first, second, third{public void show1(){ // Implementing first interface method declaration
System.out.println("First interface method");}
public void show2(){ // Implementing second interface method declaration
System.out.println("Second interface method");}
public void show3(){ // Implementing third interface method declaration
System.out.println("Third interface method");}
}class GroupInterfaceDemo{
public static void main(String args[]){
forth f = new forth();f.show1();
f.show2();
f.show3();
}
}
The output of this program is….
First interface methodSecond interface method
Third interface method
Interfaces Verses Abstract Classes
- An interface looks like an abstract class, but there are some differences between these two as shown below:
- An abstract class is an incomplete class that requires further specialization. An interface is just a specialization or prescription for the behavior.
- A class can implements several interfaces at once, whereas a class can extend only one parent class.
- An abstract class is generally used where you want to initiate a hierarchy of ore specialized classes. On the other hand, an interface is used where you say – “I need to be able to call methods with these signatures in your classes.”
Packages
Like an ordered library provides books, a package provides organizes related classes into groups. In other words a package is a collection of classes and interfaces of similar nature. Two packages may have classes of same name. But within a package, each class has unique name.
Java provides two types of packages:
- Built-in Packages
- User-defined Packages
Before the discussion of these packages let us study the import statement.
The import Statement
Java Package, irrespective of built-in or user-defined, can be used in a program by using the import statement.
Syntax
The import statement is used as:
import PackageName.*;// importing an entire package PackageName
import PackageName.ClassName;// importing a class ClassName from package PackageName
Example
import java.awt.*;import java.awt.Button;
Here a dot separates an element. This convention shows that a hierarchy has been followed while creating these built-in packages. The first statement imports all the classes or interfaces of the awt package. The second statement imports Button class from the awt package.
After importing the above package, you can create an object of the Button class as:
Button b = new Button(“Yes”);
However if you don’t want to import a package then you create an object of the Button class as:
java.awt.Button b = new java.awt.Button(“Yes”);
Naturally the first statement looks better than second.
- Built-in Packages
- User-defined Packages
User-defined Packages
You can create your own packages in order to organize your related classes. Such packages are known as user-defined packages.
Syntax
A user-defined package is created by writing the first statement in a Java program as:
package PackageName;
Here package is the reserved word and PackageName is the name of the user-defined package. You can make only one package declaration in a source Java program. After the package declaration all the classes declared within that file will belong to the specified package.
Example
package mypackage;
public class FirstClass{ // ….
}
Save this source file as FirstClass.java. When you compile this file – FirstClass.java, its class file – FirstClass.class is stored in the same directory with the name of the package. Now if you want to use the classes of this package then go to one level higher and then import a class or all classes of this package - mypackage.
Let your mypackage has the following path:
C:\>jdk\mypackage
And your FirstClass.java and FirstClass.class files are stored in C:\>jdk\mypackage\ then before using the classes of mypackage you must go to one level higher, say:
C:\>jdk
And then import the classes of mypackage, as shown below:
import mypackage.*;
public class FirstDemo{
public static void main(String args[]){
FirstClass f = new FirstClass()
}
}
S
ave this file as FirstDemo.java in C:\jdk\ directory and then compile and execute this file. If you do not go up one level higher and try to import the classes of mypackage, say you store the file FirstDemo.java in directory C:\>jdk\mypackage\ then you would certainly encounter the following error message:
“can’t find class FirstClass”