Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Interface

So, far we have learnt if there is at-least one abstract behaviour in a class the entire class has to marked as abstract. That is the concept of abstract class. So in an abstract class there can be abstract(undefined) as well as non abstract(defined) methods.

Now the concept of Interface takes us one step ahead of abstraction.

i.e In an Interface all the behaviours/methods should be abstract. In other words it's a 100% abstract class.


Interface :

An Interface is a 100% abstract class where all the methods must be abstract(i.e. undefined). Just like the abstract class an interface cannot be instantiated.


Story :

A new god came who was the designer of fish. And fish will breathe Oxygen from water. Now, the Supreme god was in problem, as he made this rule that all living beings will breathe oxygen from air.



abstract class LivingBeing{

  void breathe(){
    System.out.println("Breathes oxygen from air.");
  }

  abstract void sound();// The sound method is incomplete.

}

Now, the breathe() method was not fixed as fish breathes oxygen from water. So, the master god changed his class to an Interface:


interface LivingBeing{

  void breathe(); // Undefined

  void sound(); // Undefined
}


But we have learnt if a method is not defined we need to mark it as abstract. But in the above case we didn't do it, why?

It's because 'LivingBeing' is now an interface. And as I said, in an interface by default all the behaviours/methods are declared as abstract. So, in this case the compiler converts it for you to abstract :


interface LivingBeing{

  public abstract void breathe(); // Undefined

  public abstract void sound(); // Undefined

}


And below is the declaration of Fish class:



class Fish implements LivingBeing{

  public void breathe(){

    System.out.println("Breathes oxygen from water.");
  }

  public void sound(){

    System.out.println("It squeaks.");
  }
}

So, in the above scenario we have seen the 'implements' keyword :


class Fish implements LivingBeing

Since, LivingBeing is an interface 'implements' keyword has to be used instead of 'extends'.

So, here we are using Interface Inheritance.


Story :

And the beauty of it is, the Supreme god had declared this rule that all living beings should make some kind of sound to communicate and breathe to survive. And his fellow gods has to obey this rule by defining this behaviour in their own classes. Thus we have seen in Fish class, the sound() and breathe() behaviour had to be defined else it would have ended up with some kind of Error.

Now, let us write the class which creates the objects :


Class ApplicationTest{
  public static void main(String[] arg){

    Fish fish = new Fish();

    fish.breathe();
    fish.sound();
  }
}

Output :


   Breathes oxygen from water.
   It squeaks.

Multiple Interface Inheritance

Although multiple Inheritance is not supported by java(i.e. A java class cannot inherit from two java classes). However, multiple Interface Inheritance can be achieved in java.


Let us look at the below example :


Story :

Say there is a master God who has prepared an Interface named 'LivingOrganism' with two behaviors/methods, breathe(which is already there in 'LivingBeing' Interface) and move (Like a fish swims, birds fly and humans walk).

interface LivingBeing{

   public abstract void breathe(); // Undefined

   public abstract void sound(); // Undefined

}


interface LivingOrganism{

   public abstract void breathe(); // Already present in 'LivingBeing' interface.

   public abstract void move(); // New method

}

And below is the declaration of Fish class which implements both LivingBeing and LivingOrganism:


class Fish implements LivingBeing, LivingOrganism{

  public void breathe(){

    System.out.println("Breathes oxygen from water.");
  }

  public void sound(){

    System.out.println("It squeaks");
  }

  public void move(){

    System.out.println("It swims");
  }
}

In the above class, the move() method is newly defined. Although breathe() is present in both LivingBeing and LivingOrganism. But there is only one definition for breathe().


public void breathe(){

   System.out.println("Breathes oxygen from water.");
}

Now, let us write the class which creates the objects :


Class ApplicationTest{
  public static void main(String[] arg){

    Fish fish = new Fish();

    fish.breathe();
    fish.sound();
    fish.move();
  }
}

Output :


   Breathes oxygen from water.
   It squeaks.
   It Swims.

Note : An Interface can extend another Interface.
   A class can implement an Interface and extend a class.
   A class can implement multiple Interfaces.