Learn interface in java

Welcome to flower brackets blog. Today we are going to learn interface in java.

learn interface in java

Interface is a collection of abstract methods. These abstract methods does not have definition and when interface is inherited each and every function will be overridden defined in the interface.

Also read – learn abstraction in java

The interface is used to implement multiple inheritance in java.

In java programming,

  • An interface stipulates what class must do and not how to. Interface is a reference type alike a class.
  • Interface is a blueprint of class.
  • Interface represents IS-A relationship.
  • Interface can contain only method signatures, constants, default methods, nested types and static methods.
  • Interfaces cannot be instantiated.
  • Interfaces can only be implemented by class or extended by other interface.
  • All methods and fields in interface are public, static and final by default and declared with empty body.
  • To declare an interface we should use interface keyword.
  • Any class which implements interface should implement all methods declared in interface.
  • In Java 8 version, interface can have static and default methods.
  • Meanwhile in Java 9 interface can have private methods.

Declaring an interface – Syntax

Interface <interfaceName> 
{
   // fields are static, final and 
   // public by default.
   // methods are abstract and public  
}

interface print
{
   void show(); 
} 

class A implements print
{ 
   public void show()
   {
      System.out.println("Hello World!!");
   }

   public static void main(String args[])
   {
      A obj = new A(); 
      obj.show(); 
   }
}

Output:

Hello World!!


How to implement an interface?

In the below example CarDriving interface has one abstract method, driving(). This abstract method implementation we can see in Maruti class and Audi class.

interface CarDriving
{
   void driving();
}

class Maruti implements CarDriving
{ 
   public void driving()
   {
      System.out.println("driving maruti 800");
   } 
}

class Audi implements CarDriving
{ 
   public void driving()
   {
      System.out.println("driving Audi A7");
   } 
}

class Test
{ 
   public static void main(String args[])
   {
      CarDriving cd = new Audi();
      cd.driving(); 
   }
}

Output:

driving Audi A7


Now in the below example we have one method called accelerate(). The implementation is provided by Audi and BMW classes like this,

interface Vehicle
{ 
   void accelerate(); 
}

class Audi implements Vehicle
{ 
   public void accelerate()
   {
      System.out.println("Audi car accelerating...");
   } 
}

class BMW implements Vehicle
{ 
   public void accelerate()
   {
      System.out.println("BMW car accelerating...");
   } 
}

public class InterfaceDemo 
{
   public static void main(String[] args) 
   {
      Vehicle obj = new Audi();
      obj.accelerate();
   }
}

Output:

Audi car accelerating…

In the above example we have declared Vehicle interface and we can see the implementation in the class Audi and BMW.

Now after learning above concepts with example the relationship between a class and interface is a class implements an interface not extends.


Java multiple inheritance

A class implementing more than one interface or interface extending more than one interface is multiple inheritance.

interface X
{ 
   void display(); 
} 
interface Y
{ 
   void print(); 
}
public class TestInterface implements X, Y
{ 
   public void display()
   {
      System.out.println("Hello World!!");
   }

   public void print()
   {
      System.out.println("Interface example");
   }
   public static void main(String[] args) 
   {
      TestInterface test = new TestInterface(); 
      test.display(); 
      test.print();
   }
}

Output:

Hello World!!
Interface example


Multiple inheritance is not supported in java. Why?

The reason is,

class X
{
   public void print()
   {
   }
}
class Y extends X
{
   public void print()
   {
   }
}
class Z extends Y
{
   public void display()
   {
   }
}
class Demo
{
   public static void main(String args[])
   {
      Z obj1 = new Z();
      Z.display();
      Z.print();
   }
}

In java, method definition is searched from the object we have created in the main method, that is, we have created Z class object.

We have called from Z object. Now first, display() method is searched in Z class. If there is no method definition it goes to next immediate class.

Even there if method is not found it goes to next immediate class and so on so forth. Till method definition is not found.

Also read – learn inheritance in java

Similarly for print() method. In the above example class X and Y have same method name. Then there is ambiguity of which method to call.

So to solve the process of ambiguity java does not support multiple inheritance. But it supports multiple inheritance with the help of interfaces.


Default methods in Java 8

Previously, interface could have only abstract methods and the implementation code of these methods in separate class.

In Java 8 we can have default methods in interface which helps java programmers to add new methods to the interface. First let’s learn why do we use default methods,

Why do we use default methods?

For example, (before java 8) there are 5 classes. Class P, Q, R, S, T and U. Say all these classes implements an interface DemoInterface.

I add a new method printMethod() to DemoInterface interface. Now I have to change code in all the above classes which implements above interface.

Because Class P, Q, R, S, T and U implements DemoInterface interface. It’s easier for me to change the code in five classes.

Imagine where I have hundreds of classes which implements DemoInterface interface. It would be impossible to change code in all these classes.

This was a issue and now Java 8 has introduced a new concept of default methods. This default method allow interfaces to have methods with implementation without affecting classes which implement the interface.

Default method can be added to any existing interface and it is not mandatory for implementation classes to implement default methods.

By using default method, existing interface use the lambda expressions without executing methods in the implementation class and hence provide backward compatibility.

NOTE :

Default methods in java 8 are also called as virtual extension methods or defender methods.

interface Vehicle
{ 
   // abstract method
   public void accelerate();
   // default method
   default void message()
   {
      System.out.println("Default method");
   }
}

class BMW implements Vehicle
{ 
   // implementation of accelerate abstract method
   public void accelerate()
   {
      System.out.println("BMW car accelerating...");
   } 
}

public class InterfaceDemo 
{
   public static void main(String[] args) 
   {
      Vehicle obj = new BMW();
      obj.accelerate();
      // here default method is executed 
      obj.message();
   }
}

Output:

BMW car accelerating…
Default method


Static methods in Java 8

In Java 8 we have static methods in interface which are similar to static methods of classes. Let’s see a simple java program to demonstrate static methods in java 8,

Static methods in java 8 are similar to default method and the only difference is we cannot override static methods in the implementation classes.

interface Shapes
{ 
   // abstract method
   public void area(int a);
   // static method 
   static void print()
   {
      System.out.println("Static method execution");
   } 
}

class Square implements Shapes
{ 
   // implementation of area abstract method 
   public void area(int a)
   {
      System.out.println("Area of square : " + a * a);
   } 
}

public class InterfaceStaticDemo 
{
   public static void main(String[] args) 
   {
      Shapes sh = new Square();
      sh.area(5);
      // here static method executes 
      Shapes.print();
   }
}

Output:

Area of square : 25
Static method execution

Related Posts