Java Interface

The dictionary meaning of interface is “A point where two systems, subjects, organizations, etc., meet and interact.”

Interface in the real world

You can see a number of interface examples. Let us take the example of a TV. You press the change channel button of the TV remote and the channel is changed. Here remote acts as an interface between you and the TV.

Interface in Java

The interface is a way of implementing 10

Syntax

Interface Interface_Name {

                //abstract methods

                //static final data members

}

An interface cannot be instantiated, it can be implemented by classes. To implement an interface implement keyword is used. Interface forms a contract with your class that forces your class to have all methods defined by the interface must appear in the class. This all-enforce check is done at compile time by the compiler i.e. A class must implement all of the interface methods if it implements an interface.

Syntax

class ClassName implements InterfaceName {

              //Implementation of Interface Methods

             //Block of Class Code

}

Important Points:

  1. A class extends another class.
  2. An interface extends another interface.
  3. A class implements an interface.

Java Interface Example

package com.w3schools;

interface ShowDetails {
    void showDetail(String name, int age);
}

public class InterfaceTest implements ShowDetails {
    
    @Override
    public void showDetail(String name, int age) {
        System.out.println("Name = " + name);
        System.out.println("Age = " + age);
    }

    public static void main(String args[]) {
        InterfaceTest obj = new InterfaceTest();

        obj.showDetail("Saralyn", 26);
    }
}

Output

Name = Saralyn
Age = 26

Multiple Inheritance in Java

  1. A class can implement multiple interfaces.
  2. An interface can extend multiple interfaces.

Example

package com.w3schools;

interface ShowAge {
    void age(int age);
}

interface ShowName {
    void name(String name);
}

public class InterfaceTest implements ShowAge, ShowName {
    @Override
    public void age(int age) {
        System.out.println("Age = " + age);
    }

    @Override
    public void name(String name) {
        System.out.println("Name = " + name);
    }

    public static void main(String args[]) {
        InterfaceTest obj = new InterfaceTest();

        obj.name("Roy");
        obj.age(26);
    }
}

Output

Name = Roy
Age = 26

Why multiple inheritance is possible in the case of interfaces but not possible in the case of classes?

Multiple inheritance is possible in the case of interfaces but not possible in the case of classes because there is no ambiguity problem in the case of interfaces as implementation is provided by the class that implements interfaces not by interfaces themselves.

Example

package com.w3schools;

interface Show {
    void show(String name, int age);
}

interface Detail {
    void show(String name, int age);
}

public class InterfaceTest implements Show, Detail {
    @Override
    public void show(String name, int age) {
        System.out.println("Name = " + name);
        System.out.println("Age = " + age);
    }

    public static void main(String args[]) {
        InterfaceTest obj = new InterfaceTest();

        obj.show("Daniel", 36);
    }
}

Output

Name = Daniel
Age = 36

An interface extends another interface Example

package com.w3schools;

interface ShowAge {
    void age(int age);
}

interface ShowName {
    void name(String name);
}

interface showDetails extends ShowAge, ShowName {
    void rollNo(int rollNo);
}

public class InterfaceTest implements showDetails {

    @Override
    public void age(int age) {
        System.out.println("Age = " + age);
    }

    @Override
    public void name(String name) {
        System.out.println("Name = " + name);
    }

    @Override
    public void rollNo(int rollNo) {
        System.out.println("RollNo = " + rollNo);
    }

    public static void main(String args[]) {
        InterfaceTest obj = new InterfaceTest();

        obj.name("Jon");
        obj.age(25);
        obj.rollNo(4);
    }
}

Output

Name = Jon
Age = 25
RollNo = 4
  • Methods of an interface are public abstract by default.
  • The Data members of an interface are public static final by default.
  • The interface is abstract by default that’s why no abstract keyword is used in the interface declaration.

Marker/Tagging Interfaces

An interface with no methods is known as a marker or tagged interface.

Why marker interface used?

It provides some useful information to the JVM/compiler so that the JVM/compiler performs some special operations on it. It is used for better readability of code.  Example: Serializable, Cloneable, etc.

public interface InterfaceName {

}

Let us understand it with an example. There are a number of colleges from which some colleges are of A grade. We have created an AGradeCollegeMarker interface which contains no method and only informs the JVM that it is an A grade college. Every A-grade college has to implement AGradeCollegeMarker. In the TestCollege class, the tester method will print “A grade college.” if the object belongs to A grade college.

Example

public interface AGradeCollegeMarker {
 
}
public class College1 implements AGradeCollegeMarker{
    //Your Code Block
}
public class College2 {
    //Your Code Here
}
public class TestCollege {
    static void tester(Object obj){
        if (obj instanceof AGradeCollegeMarker) {
                       System.out.println("A grade college.");
                 }
    }
 
    public static void main(String args[]){
        College1 obj1 = new College1();
        College2 obj2 = new College2();
 
        //test college objects
        tester(obj1);
        tester(obj2);
    }
}

Output

A grade college.

Why an interface can’t have a constructor?

Because the interface does not have any instance fields so nothing to construct. Now the question arises of how an interface can be inherited without a constructor because a subclass constructor is called a super superclass constructor. Now we have two cases. The first case is when an interface extends other interfaces, in this case, there is no issue because no interface has a constructor and hence no super class constructor call. The second case is when a class implements an interface, in this case, there is no inheritance because the class implements the interface and does not extend the interface.

Please follow and like us:
Content Protection by DMCA.com