×
☰ See All Chapters

Java interface

TV remote is an interface between TV and viewer; likewise java interface is an interface between specification and implementation. Java interface does not contain any implementation. It is used for setting the standards for the implementation classes to do the implementation. Interface should have only 2 members, we cannot declare other members inside interface. Following are the members we can have inside interface.

    1. public static final variables 

    2. public abstract methods 

Java compiler adds public and abstract keyword to interface methods and static final keyword to variables. It is not required to add explicitly.

Interface Syntax

interface Interfacename {

datatype variableName = value;

returntype methodname(parameter list);

}

Interface Example

interface Demo {

        int num = 10;

        int add(int x, int y);

        int multiply(int x, int y);

}

An interface can be empty without any members, such interfaces are called marker interface. JVM performs special operations on marker interface. E.g.: Serializable, Cloneable, Remote etc. Interface cannot be instantiated means you cannot create object of interface, but you can declare a reference variable of it. Interface reference variable can refer any implementation objects.  Below is the syntax to implement interface:

class classname implements interface ,interface... {

// class-body

}

A class can implement any number of interfaces. From this we can achieve multiple inheritance in java. The class which implements interface can extend any other class as below.

class classname extends superclass implements interface ,interface... {

// class-body

}

Extending super class is optional. If class implements more than one interface then they should be separated by comma.

Example:

class DemoImpl implements Demo {

        public int add(int x, int y) {

                int z = 0;

                z = x + y;

                return z;

        }

 

        public int multiply(int x, int y) {

                int z = 0;

                z = x * y;

                return z;

        }

}

 

The methods that implement an interface abstract must be declared public. Also, the type signature of the implementing method must match exactly the type signature of abstract methods specified in the interface definition. When a class is implementing the interface, then that class must override all the abstract method in the interface otherwise class should be declared as abstract. When a class is implementing interface, then all final static variables of the interface will be inherited into subclass. Class implementing interface may contain even main method also.

class A {

        void meth() {

                System.out.println("Hello");

        }

}

 

interface Callback1 {

        void callback1(int param);

}

 

interface Callback2 {

        void callback2(int param);

}

 

class Client extends A implements Callback1, Callback2 {

        public void callback1(int p) {

                System.out.println("callback1 called with " + p);

        }

 

        public void callback2(int q) {

                System.out.println("callback2 called with " + q);

        }

}

 

class TestInterface {

        public static void main(String args[]) {

                Callback1 c = new Client();

                c.callback1(42);

                Callback2 d = new Client();

                d.callback2(52);

        }

}

      Output:

callback1 called with 42

callback2 called with 52

 


All Chapters
Author