Interfaces in Java (Part 8)

Pre-requisites : Inheritance , Abstraction 


Java Interface

Another way to achieve abstraction is through Interfaces.

There are only abstract methods in Interfaces and none of them have method body. It is also used to achieve multi-inheritance.

It provides Total Abstraction (100% abstraction).




Declaring an Interface :

Rules for declaring Interface:
  • Like abstract classes, interfaces cannot be used to create objects.
  • Interface methods do not have a body.
  • On implementation of an interface, you must override all of its methods.
  • Interface methods are by default abstract and public
  • Interface attributes are by default publicstatic and final
  • An interface cannot contain a constructor.

Syntax:-
 interface interface_name {  
  //abstract methods only  
 }  


Implementation Of Interface

1. Single-Level Inheritance


A Class extends a Class , An Interface extends an Interface too , only A Class implements an Interface.

As a Class inherits another Class in Inheritance , the working of Interface is quite similar as it is inherited by keyword implements.


Eg:-
 //declaring an interface  
 interface Bank {  
   
      // methods declared are by default abstract and public  
      public abstract int getRateOfInterest();  
 }  
   
 class ICICI implements Bank {  
   
      // @Override interface method  
      public int getRateOfInterest() {  
           return 9;  
      }  
 }  
   
 class SBI implements Bank {  
   
      // @Override interface method  
      public int getRateOfInterest() {  
           return 8;  
      }  
 }  
   
 public class MyClass {  
      public static void main(String[] args) {  
   
           /*  
            * As interface cannot be instantiated we have to instantiate a Class  
            */  
           ICICI b1 = new ICICI();  
           System.out.println("Rate Of Interest Of ICICI Bank : "+ b1.getRateOfInterest() + "%");  
   
           SBI b2 = new SBI();  
           System.out.println("Rate Of Interest Of SBI Bank : "+ b2.getRateOfInterest() + "%");  
      }  
 }  
   
Output:-
 Rate Of Interest Of ICICI Bank : 9%  
 Rate Of Interest Of SBI Bank : 8%  


2. Multiple Inheritance

If a Class implements multiple Interfaces, or an Interface extends multiple Interfaces, it is called as Multiple Inheritance.

Eg:-
 //declaring an interface  
 interface Engine {  
   
      // methods declared are by default abstract and public  
      public abstract int getEngineInfo();  
 }  
   
 interface Gear {  
      public abstract int getGearInfo();  
 }  
   
 // Class implementing two interfaces  
 class Honda implements Engine , Gear {  
   
      // @Override interface methods  
      public int getGearInfo() {  
           return 3;  
      }  
   
      // @Override interface methods  
      public int getEngineInfo() {  
           return 1000;  
      }  
 }  
   
 class Hyundai implements Engine , Gear {  
   
      // @Override interface methods  
      public int getGearInfo() {  
           return 2;  
      }  
   
      // @Override interface methods  
      public int getEngineInfo() {  
           return 500;  
      }  
 }  
   
 public class MyClass {  
      public static void main(String[] args) {  
   
           /*  
            * As interface cannot be instantiated we have to instantiate a Class  
            */  
           Honda car1 = new Honda();  
           System.out.println("The Car is at gear " + car1.getGearInfo());  
           System.out.println("The Speed of Car is " + car1.getEngineInfo()+ " kmph.");  
   
           Hyundai car2 = new Hyundai();  
           System.out.println("The Car is at gear " + car2.getGearInfo());  
           System.out.println("The Speed of Car is " + car2.getEngineInfo()+ " kmph.");  
      }  
 }  
Output:-
 The Car is at gear 3  
 The Speed of Car is 1000 kmph.  
 The Car is at gear 2  
 The Speed of Car is 500 kmph.  


Inheritance Of Interface

Interfaces can also be inherited like classes. When we implement Child Interface , we can also access the attributes of Parent Interface as it is inherited from it.

Eg:-
 //Parent Interface  
 interface Print {  
      public abstract void print();  
 }  
   
 //Child Interface  
 interface Show extends Print {  
      public abstract void show();  
 }  
   
 class Object implements Show {   //implements both interfaces  
      public void print() {  
           System.out.println("Printing object.");  
      }  
        
      public void show() {  
           System.out.println("Showing object.");  
      }  
 }  
   
 public class MyClass {  
      public static void main(String[] args) {  
           Object obj=new Object();  
           obj.print();  
           obj.show();  
      }  
 }  
Output:-
 Printing object.  
 Showing object.  



Default and Static Methods of Interface

Since Java 8 , Interfaces can also declare default and static methods which can have bodies too . These methods are not forcibly overridden and can be directly called from the Main Class.

Eg:-
 interface Draw {   
     //abstract method  
     public abstract void letsDraw();   
   
     //default method  
     default void print() {  
       System.out.println("Printing a Rectangle");  
   }  
   
     //static method  
     static int cube(int x) {  
       return x*x*x;  
   }   
 }   
   
 class Rectangle implements Draw {   
     public void letsDraw() {  
       System.out.println("Drawing a Rectangle");  
   }   
 }   
    
 public class MyClass {   
   public static void main(String args[]){   
     Rectangle rec=new Rectangle();   
     rec.letsDraw();   
     rec.print();  
     System.out.println(Draw.cube(3));   
   }  
 }   
Output:-
 Drawing a Rectangle  
 Printing a Rectangle  
 27  

To view Differentiating Table between Abstraction Class & Interfaces , click here.


< Previous Next >