Learn Interface In Java

Welcome to flower brackets blog. Today we are going to learn interface in java. In this post we will discuss interfaces in java with an example.

Learn Interface In Java

What is interface? Learn interface in java

An interface, like class, is a group of related methods and variables. But the methods and variables declared are by default abstract (only signature).

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.

Syntax

Interface <interfaceName> 
{
   // by default
   abstract fields
   abstract methods   
}

Here is the example where methods in interface are public and abstract and fields are public, static and final by default,

// student.java

interface student
{
   int marks = 50;
   void print();
}

// student.class

interface student
{
   public static final int marks = 50;
   public abstract void print();
}

How to implement an interface?

I have two interfaces,

public interface InterfaceX
{
   void printHello();
}

public interface InterfaceY
{
   void printHey();
}

InterfaceX has abstract method printHello and InterfaceY has abstract method printHey. Below I have DemoClass class which implements above two interfaces,

public class DemoClass implements InterfaceX, InterfaceY
{
   @Override
   public printHello()
   {
      System.out.println("Hello");
   }
   
   @Override
   public printHey()
   {
      System.out.println("Hey");
   }
}

So DemoClass class must provide implementation of printHello and printHey method. In java we use implements keyword to implement interface.

A class can implement more than one interfaces. Here’s the complete java program on interface in java,

public interface InterfaceX
{
   void printHello();
}

public interface InterfaceY
{
   void printHey();
}

public class DemoClass implements InterfaceX, InterfaceY
{
   @Override
   public printHello()
   {
      System.out.println("Hello");
   }

   @Override
   public printHey()
   {
      System.out.println("Hey");
   }
}

// main class
public class InterfaceDemo
{
   public static void main(String[] args)
   {
      DemoClass obj = new DemoClass();
      obj.printHello();
      obj.printHey();
   }
}

Output:

Hello

Hey


Java interface example

Now in the below java interface 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.


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) I declare 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.

Here is a java program to test interface with default methods in java 8,

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.

Let’s see an java program on static methods in java 8

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
Staic method execution


Conclusion:

That’s it guys. In this post we learnt what is interface in java and how to implement interface in java. I hope you guys have understood the concept.

You can subscribe to my blog flower brackets blog if you haven’t already. Do share this post if you like.

Related Posts