What is Interface?
Interface is something which helps 2 system to interact with each other, without one system has to know the details of other.
Or in simple term we can say, it helps to achieve ABSTRACTION.
How to define the interface?
Inteface declaration consists of
Modifiers
"interface" keyword
Interface Name
Comma separated list of parent interfaces
Body
Only Public and Default Modifiers are allowed (protected and private not allowed)
public interface Bird {
public void fly();
}
interface Bird {
public void fly();
}
Comma separated list of parent interfaces (it can extend from Class) Example:
public interface NonFlyingBirds extends Bird, LivingThings{
public void canRun();
}
Why we need Interface?
1. Abstraction
Using interface, we can achieve full abstractoin means, we can define WHAT class must do, but not HOW it will do
public interface Bird {
public void fly();
}
public class Eagle implements Bird{
@Override
public void fly(){
//the complex process of flying take place here
}
}
2. Polymorphism
Interface can be used as a Data type.
We can not create the object of an interface, but it can hold the reference of all the classes which implements it. And at runtime, it decide which method need to be invoked.
public interface Bird {
public void fly();
}
public class Eagle implements Bird{
@Override
public void fly(){
System.out.println("Eagle Fly Implementation");
}
}
public class Hen implements Bird{
@Override
public void fly(){
System.out.println("Hen Fly Implementation");
}
}
public class Main{
public static void main(String args[]){
Bird birdObj1 = new Eagle();
Bird birdObj2 = new Hen();
birdObj1.fly();
birdObj2.fly();
}
}
3. Multiple Inheritance
In Java multiple inheritance is poosible only through interface only.
Diamond Problem:
public class WaterAnimal{
public boolean canBreathe(){
return true;
}
}
public class LandAnimal{
public boolean canBreathe(){
return true;
}
}
/*
Not Possible
which canBreathe to called?
*/
public class Crocodile extends LandAnimak, WaterAnimal{
}
Solution through interfaces
public interface WaterAnimal{
public boolean canBreathe();
}
public interface LandAnimal{
public boolean canBreathe();
}
// Possible
public class Crocodile implements LandAnimak, WaterAnimal{
@Override
public boolean canBreathe(){
return true;
}
}
Methods in Interface
All methods are implicit public only.
Method cannot be declared as final.
public interface Bird {
void fly();
public void hasBreak();
}
Fields in interface
Fields are public, static, and final implicitly (CONSTANTS)
You cannot make field private or protected.
public interface Bird {
int MAX_HEIGHT_IN_FEET = 2000;
}
public interface Bird {
public static final int MAX_HEIGHT_IN_FEET = 2000;
}
Interface Implementation
Overrdiing method cannot have more restrcit access specifiers.
Concrete class must override all the methods declared in the interface.
Abstract classes are not forced to override all the methods.
A class can implement from multiple interfaces.
public interface Bird {
public void fly();
}
public class Eagle implements Bird{
@Override
public void fly(){
System.out.println("Eagle Fly Implementation");
}
}
Following not possible
public class Eagle implements Bird{
@Override
protected void fly(){
System.out.println("Eagle Fly Implementation");
}
}
Example of Abstract class Implementaiton of Interface
public interface Bird {
public void canFly();
public void noOfLegs();
}
public abstract class Eagle implements Bird{
@Override
public void canFly(){
System.out.println("Eagle Fly Implementation");
}
public abstract void breakLength();
}
public class WhiteEagle extends Eagle{
@Override
public void noOfLegs(){
//implement interface method
}
@Override
public void breakLength(){
//implementing abstract class method
}
}
Nested Interface
Nested Interface declared with in another interface.
Nested Interface declared within a class.
Generally its is used to group, logical related interfaced. And Nested interface.
Rules:
A nested interface delcared within an interface must be public.
A nested interface declared within a class can have any access modifier.
When you implement outer interface, inner interface implementation is not required and vice versa.
Nested Interface in Interface
public interface Bird {
public void canFly();
public interface NonFlyingBird{
public void canRun();
}
}
Outer Interface Implementation
public class Eagle implements Bird{
@Override
public void canFly(){
System.out.println("Eagle Fly Implementation");
}
}
Inner Interface Implementation
public class Eagle implements Bird.NonFlyingBird{
@Override
public void canRun(){
System.out.println("Eagle Can Run Implementation");
}
}
Usage
public class Main{
public static void main(String args[]){
Bird.NonFlyingBird obj = new Eagle();
obj.canRun();
}
}
Nested Interface in class
public class Bird {
protected interface NonFlyingBird{
public void canRun();
}
}
Usage
public class Eagle implements Bird.NonFlyingBird{
@Override
public void canRun(){
System.out.println("Eagle Can Run Implementation");
}
}
Abstrace vs Interface
Abstract Class | Interface |
Keyword used here is "abstract" | Keyword used here is "interface" |
Child Classes need to use keyword "extends" | Child Classes need to use keyword "implements" |
It can have both abstract and non abstract method | It can have only Abstract method ( from Java8 onwards it can have default, static, and private method too, where we can provide implementation) |
It can Extend from Another class and multiple interfaces | It can only extends from other interfaces |
Variables can be static, non static, final, non final etc. | Variable are by default CONSTANTS |
Variables and methods can be private, protected, public, default | Variable and methods are by default public (In Java9, private method is supported) |
Multiple inheritance is not supported | Multiple Inheritance supported with this in Java |
It can provide the implementation of the interface | it cannot provide implementation of any other interface of abstract class. |
It can have Constructor | It cannot have Constructor |
To declare the method abstract, we have to use "abstract" keyword and it can be protected, public, and default. | No need for any keyword to make method abstract. And be default its public. |