Introduction
Although Java Possesses most of the features of C++, multiple inheritances are not supported by Java. To balance this drawback, the originators of Java have introduced the idea of interfaces. An interface consists of a set of method definitions. Any class implementing it should code for all its methods.
Interface in Java
An interface is a collection of abstract behavior that individual classes implement. It is defined as a class.
Syntax
<access specifier> <interface name>
{
<return type><method_name1> (<parameter-list>);
<return type><method_name2> (<parameter-list>);
-
-
-
<type><variable_name1>=<value>;
<type><variable_name2>=<value>;
-
-
-
}
Here access is either public or without specification & its accessibility remains.
The keyword interface implies that the following method definition forms an interface rather than a class definition. The interface should not contain implementation for its methods. Any method belonging to an interface method that is implemented in a class should be declared as a public variable. When variables are included within an interface, they become final and static, i,e variables cannot be changed by the class implementing them.
Implementation
The methods belonging to an interface are looked up during runtime. Usually, when a method of a class is called from another method, the Java compiler ensures that its definition exists and is compatible. This leads to a static and non-extensible class environment. Thus, when a method has to be used by many classes, it is pushed higher up in the hierarchy to ensure that it is available to all of them. Interfaces provide a different approach. They collect such a method and disconnect them for their hierarchy. Thus, classes can access methods of an interface or a series of interfaces.
The following program illustrates the creation and implementation of an interface.
Step 1. Open a new file in the editor, and enter the following code.
public interface Address
{
public void add(String add);
public void name(String name);
public void color(String ch);
}
Step 2. Save the file Address.java. The above code creates an interface called Address, and all classes can access it. There are three method definitions included. Any class which implements this interface will necessarily have to include code for all these three methods.
Step 3. Open another file in the editor and enter the following code.
class A implements Address
{
public void add(String a)
{
System.out.println("Inside implemented method add()");
System.out.println("Address :" +a);
}
public void name(String n)
{
System.out.println("Inside implemented method name()");
System.out.println("Name :" +n);
}
public void color(String ch)
{
System.out.println("Inside implemented method color()");
System.out.println("Favourite Color :" +ch);
}
void disp()
{
System.out.println("Method of class A disp");
}
public static void main(String args[])
{
A a=new A();
System.out.println("Inside main of class A which implements Address interfaces");
System.out.println("Calling method disp() of class A...");
a.disp();
System.out.println("Calling the implemented method name()");
a.name("Ashish Bhatnagar");
System.out.println("Calling the implemented method add()");
a.add("Ghaziabad");
System.out.println("Calling the implemented method color()");
a.color("Blue");
}
}
Output
The above program has a class A, which implements the Address interface. Note that coding for all three methods included within the interface has been done in this class. The main() method creates an object of class A and calls each of the three methods. Apart from calling them, it also calls the disp() of class A; note that the implemented methods have been declared public.
Summary
The interface is a collection of abstract behavior that individual classes can implement. It is defined as a class. Variables can be declared within an interface. The interface allows you to adapt a class from a different hierarchy to work in an existing application. The class only needs to declare it implements the interface, provides necessary methods, and can be integrated directly as if it is created for the job. Designing relatively large applications using interfaces is advisable because it makes the whole system easier to modify, extend and integrate new features.