Friday, 11 September 2015

Interface in Java

Interface in Java

An interface in java is a blueprint of a class. It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.
  • It is used to achieve fully abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.

In other words, Interface fields are public, static and final bydefault, and methods are public and abstract.
interface

Understanding relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.
relationship between class and interface

Simple example of Java interface

In this example, Printable interface have only one method, its implementation is provided in the A class.
  1. interface printable{  
  2. void print();  
  3. }  
  4.   
  5. class A6 implements printable{  
  6. public void print(){System.out.println("Hello");}  
  7.   
  8. public static void main(String args[]){  
  9. A6 obj = new A6();  
  10. obj.print();  
  11.  }  
  12. }  

Output:Hello

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
 multiple inheritance in java
  1. interface Printable{  
  2. void print();  
  3. }  
  4.   
  5. interface Showable{  
  6. void show();  
  7. }  
  8.   
  9. class A7 implements Printable,Showable{  
  10.   
  11. public void print(){System.out.println("Hello");}  
  12. public void show(){System.out.println("Welcome");}  
  13.   
  14. public static void main(String args[]){  
  15. A7 obj = new A7();  
  16. obj.print();  
  17. obj.show();  
  18.  }  
  19. }  

Output:Hello
       Welcome

Q) Multiple inheritance is not supported through class in java but it is possible by interface, why?

As we have explained in the inheritance chapter, multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class. For example:
  1. interface Printable{  
  2. void print();  
  3. }  
  4. interface Showable{  
  5. void print();  
  6. }  
  7.   
  8. class TestTnterface1 implements Printable,Showable{  
  9. public void print(){System.out.println("Hello");}  
  10. public static void main(String args[]){  
  11. TestTnterface1 obj = new TestTnterface1();  
  12. obj.print();  
  13.  }  
  14. }  
Output
Hello
As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity.

Interface inheritance

A class implements interface but one interface extends another interface .
  1. interface Printable{  
  2. void print();  
  3. }  
  4. interface Showable extends Printable{  
  5. void show();  
  6. }  
  7. class Testinterface2 implements Showable{  
  8.   
  9. public void print(){System.out.println("Hello");}  
  10. public void show(){System.out.println("Welcome");}  
  11.   
  12. public static void main(String args[]){  
  13. Testinterface2 obj = new Testinterface2();  
  14. obj.print();  
  15. obj.show();  
  16.  }  
  17. }  
Output
       Hello
       Welcome

Q) What is marker or tagged interface?

An interface that have no member is known as marker or tagged interface. For example: Serializable, Cloneable, Remote etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.
  1. //How Serializable interface is written?  
  2. public interface Serializable{  
  3. }  

Nested Interface in Java

Note: An interface can have another interface i.e. known as nested interface. We will learn it in detail in the nested classes chapter. For example:

  1. interface printable{  
  2.  void print();  
  3.  interface MessagePrintable{  
  4.    void msg();  
  5.  }  
  6. }  

No comments:

Post a Comment