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
abstractandpublic
- Interface attributes are by default
public,staticandfinal
- An interface cannot contain a constructor.
Syntax:-
interfaceinterface_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 interfaceinterfaceBank{// methods declared are by default abstract and publicpublicabstractintgetRateOfInterest(); } classICICI implements Bank{// @Override interface methodpublic intgetRateOfInterest() {return9; } } classSBI implements Bank{// @Override interface methodpublic intgetRateOfInterest() {return8; } } public classMyClass{ public static void main(String[] args) {/* * As interface cannot be instantiated we have to instantiate a Class */ICICIb1= new ICICI(); System.out.println("Rate Of Interest Of ICICI Bank :"+b1.getRateOfInterest() + "%"); SBIb2= 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 interfaceinterfaceEngine{// methods declared are by default abstract and publicpublicabstractintgetEngineInfo(); } interfaceGear{ publicabstractintgetGearInfo(); }// Class implementing two interfacesclassHonda implements Engine , Gear{// @Override interface methodspublic intgetGearInfo() {return3; } // @Override interface methods public int getEngineInfo() { return 1000; } } classHyundai implements Engine , Gear{// @Override interface methodspublic intgetGearInfo() {return2; }// @Override interface methodspublic intgetEngineInfo() { return500; } } public classMyClass{ public static void main(String[] args) {/* * As interface cannot be instantiated we have to instantiate a Class */Hondacar1= new Honda(); System.out.println("The Car is at gear" +car1.getGearInfo()); System.out.println("The Speed of Car is" +car1.getEngineInfo()+ "kmph."); Hyundaicar2= 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 Interfaceinterface{ publicabstractvoid(); }//Child InterfaceinterfaceShow extends Print{ publicabstractvoidshow(); } classObject implements Show{//implements both interfacespublic void() { System.out.println("Printing object."); } public voidshow() { System.out.println("Showing object."); } } public classMyClass{ public static void main(String[] args) { Objectobj=new Object();obj.();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:-
interfaceDraw{//abstract methodpublic abstract voidletsDraw();//default methoddefaultvoid() { System.out.println("Printing a Rectangle"); }//static methodstaticintcube(intx) { returnx*x*x; } } classRectangle implements Draw{ public voidletsDraw() { System.out.println("Drawing a Rectangle"); } } public class MyClass { public static void main(String args[]){ Rectanglerec=new Rectangle(); rec.letsDraw(); rec(); System.out.println(Draw.cube(3)); } }
Output:-
Drawing a Rectangle
Printing a Rectangle
27
To view Differentiating Table between Abstraction Class & Interfaces , click here.



