Interface With Example In Java

An Interface in JAVA is able to achieve 100% abstraction as it only contains those methods which has no implementation (i.e. methods without body). In other words we can say interface can only contain method signature and fields. Starting JAVA 8 default and static methods can have implementation in the interface.

Fields declared in Interface are public, static & final by default and methods are public abstract by default.

Interfaces are the blueprints of a class. They are never made to represent objects. They just act as a medium between a java program and the concept of java programming language.

According to Sun Microsystem: Interfaces are the contract between a programmer and a java programming language.


Explanation Of Interface With Example:

Just like abstraction, interface also hides implementation details and only show the functionality to the user. But there is a difference between abstraction and interface. The first difference is all methods describe inside interface doesn’t contain any implementation while methods inside abstraction can be with and without implementation. The other key difference is all methods defined in Interface has to be implemented by a class which is implementing it while in abstraction case class which is extending abstract class can only override those method which is needed.

Example 1: We will use the same example of sending SMS to explain Interface which we used in abstraction also. The reason we are using same example because Interface helps to achieve full abstraction:

When a user send SMS, he simply enter the address, type message in the text box and send it. He doesn’t know what is happening behind the scene when this message is getting send to the other user. So here implementation details has been hided from the user and only functionality is presented to user. This is achieved in JAVA either through Abstraction or Interface.

Example 2: Let us take a real world example for better understanding of interface. Suppose we may want to take a interface called Fly and this interface may have methods like goDown() and goForward().  These methods would not contain any logic (i.e. without any implementation). But each class that implements interface Fly would have to implement those two methods. You could have classes for flying birds like Duck and Finch which implements interface Fly. So if you want to keep the list of instances that can fly, you just create a list that contains the items of type Fly.


DECLARING INTERFACE:

Interfaces are declared by specifying a keyword “interface”. For example:

interface Fly
{
  void goForward();
  void goDown();
}

Important Note: Remember that in interface all the methods are public abstract by default and these methods are not having any body or implementation.


IMPLEMENTATION OF INTERFACE:

Class implements interface by providing body i.e. implementation to all the methods that were declared in interface.

  • implements keyword is used for implementing Interface
  • Since all the methods of Interface does not have body at all. So these methods must be implemented in some other class before accessing them.
  • Class which is implementing Interface must override/implement all the declared methods inside interface

Example: Below is an example to shows how a class implements an interface. This class provides the body of all the methods that were declared in interface.

Important Note: Class implements interface but an interface extends another interface. After below example we will also show you how interface extends another interface.

Step 1: Create interface Fly which has two method goForward() and goDown().

interface Fly
{
    void goForward();
    void goDown();
}

Step 2: Lets create a class bird Duck which implements Fly

class Duck implements Fly
{
    public void goForward()

    {
        System.out.println("Duck going forward");
    }

    public void goDown()
    {
        System.out.println("Duck going down");
    }


}

Step 3: Now lets create one more class for bird Finch which also implements Fly

public class Finch implements Fly {

    public void goForward() {
        System.out.println("Finch is going forward");

    }

    public void goDown() {

        System.out.println("Finch is going down");
    }
}

Step 4: Lets create class Bird which will be our main class. Here we will instantiate both Duck and Finch class:

public class Bird {

    public static void main(String[] args) {

        Duck duck = new Duck();
        duck.goForward();
        duck.goDown();

        Finch finch = new Finch();
        finch.goForward();
        finch.goDown();

    }

}

OUTPUT:

Duck going forward
Duck going down
Finch is going forward
Finch is going down

EXTENDING INTERFACE:

One interface can extends another interface. Remember interface cannot implements other interface, only class can do that.

Below example shows how interface extends another interface. In this example we use two interfaces one is RollNoDetails and other is PersonDetails which extends RollNoDetails:

Step 1: Create a RollNoDetails interface having one rollNo() method:

public interface RollNoDetails {

    void rollNo();

}

Step 2: Now create one more interface PersonDetails which extends RollNoDetails and declare name() method in it.

Because we use extends keyword so methods of interface RollNoDetails will now be inherited in PersonDetails. This means interface PersonDetails now has two methods name() and rollNo(). This both the method must be implemented when any class interface PersonDetails.

public interface PersonDetails extends RollNoDetails {
    
    void name();

}

Step 3: Now create a class name Details which implements PersonDetails. Here we will have to provide the body of both the methods name() and rollNo(), since methods of interface RollNoDetails were inherited in PersonDetails.

Also Details will be our main class. So here we will create object of Details class and run both the methods on it.

public class Details implements PersonDetails{
    
    public void name(){
        System.out.println("AbhiAndroid");
    }
    
    public void rollNo(){
        System.out.println("98639012");
    }

    public static void main(String[] args) {
        
        Details details = new Details();
        System.out.print("Name: ");
        details.name();
        System.out.print("Roll No: ");
        details.rollNo();
    }
}

OUTPUT:

Name: AbhiAndroid
Roll No: 98639012

DIFFERENCE BETWEEN INTERFACE AND CLASS:

INTERFACE:

  • Interface cannot be instantiated.
  • In interface, each object created after implementing will have the same state.
  • Every object will have to define its own behavior by implementing the contract defined.
  • An interface cannot inherit any classes while it can extend many interfaces.
  • All the variables are public static final by default and a value needs to be assigned at the time of definition in case of interface.
  • All the methods are public abstract by default and they will not have a definition.

CLASS:

  • Class can be instantiated.
  • In class, each object created will have its own state.
  • Every object will have the same behavior unless overridden.
  • A class can inherit only one class and can implement many interfaces.
  • All the variables are instance by default unless otherwise specified in case of class.
  • All the methods should be having a definition unless decorated with an abstract keyword.

IMPORTANCE OF INTERFACE:

Following are the importance or benefits of interface:

  • Interface provides a contract for all the implementation classes, so in terms of interface it’s good to code because implementation classes cannot remove the methods we are using.
  • Since a java class can implements multiple interfaces at a time so it’s better to use interfaces as super class in most of the cases.
  • The benefit of interface is it allows multiple inheritance that thing is not possible with concrete classes and abstract classes.
  • With interfaces, implements keyword is to be used for implementing. Implements tells the compiler that whatever classes are there after implementation, they are interfaces.
  • An interface gives template structure of methods from which new classes can be developed easily.

IMPORTANT POINTS TO REMEMBER:

  • Interfaces are implicitly abstract so they cannot be instantiated.
  • If you want to define the non static function in a interface with a body then you have to make it default by using a default keyword.
  • Default keyword can only be used within a interface only.
  • If you give only the prototype of any method in an interface then by default it will become public and abstract.
  • A child class can implement more than one interfaces simultaneously.
  • If a child class is having a same default method from more than one interfaces and the child class has not overridden that method then it is a compilation error.
  • If a child class is having a same abstract method from more than one interfaces then it has to override once.
  • One interface can extend another interface.
  • If one interface is extending the another interface and it is getting the same method from a parent interface which are already define in a parent interface then priority always goes to the methods of child interface.
  • One interface can extend more than one interface simultaneously and this is the situation we can say in java there is multiple inheritance in case of interfaces.

MARKER OR TAG INTERFACE:

Marker interface in java is a interfaces with no fields or methods. In simple words, empty interface is called marker interface. Example of marker interface is Serializable, Clonnable and  Remote interface. Marker interface is also called tag interface in java.

Declaring marker interface:

public interface serializable

{

}

Why we use marker interface?

  • Marker interface used as an tag to inform a message to the java compiler so that it can add special behavior to the class implementing it. Java marker interface has no members in it.
  • Lets take the java.io.Serializable marker interface. It does not have any member defined in it. Whenever a class is to be serialized, you should intimate the java compiler in some way that there is possibility of serializing this java class. In this scenario, marker interfaces are used. The java class which may be serialized has to implement the marker interface I.e java.io.Serializable. In such way, we are intimating the java compiler.

Leave a Reply

Your email address will not be published. Required fields are marked *