Dremendo Tag Line

Interface in Java Programming

OOPs in Java

In this lesson, we will understand what is Interface in Java and how to use it in class object programming.

What is Interface in Java?

In Java, an interface is similar to a class containing method signatures and constant variables but without implementation, which means that the methods declared in the interface can't have a body.

It is used to specify the behavior of a class, allowing multiple classes to share a common set of methods and constants.

Interfaces are also useful for creating a common interface for multiple implementations of the same concept.

To use an interface, a class must implement it using the keyword implements in the class definition, followed by the interface's name. The class must then provide an implementation for all of the methods defined in the interface.

video-poster

Example

// Create an interface called MyInterface having two methods in it.
interface MyInterface
{
    public void method1();
    public void method2();
}

// Implement the interface MyInterface in a class called MyClass1
class MyClass1 implements MyInterface
{
    public void method1()
    {
        // Implementation for method1
        System.out.println("I am method1 in MyClass1");
    }
    public void method2()
    {
        // Implementation for method2
        System.out.println("I am method2 in MyClass1");
    }
}

// Implement the interface MyInterface in a class called MyClass2
class MyClass2 implements MyInterface
{
    public void method1()
    {
        // Different implementation for method1
        System.out.println("I am method1 in MyClass2");
    }
    public void method2()
    {
        // Different implementation for method2
        System.out.println("I am method2 in MyClass2");
    }
}

public class Example
{
    public static void main(String args[])
    {
        MyClass1 obj1 = new MyClass1();
        MyClass2 obj2 = new MyClass2();
        obj1.method1();
        obj1.method2();
        obj2.method1();
        obj2.method2();
    }
}

Output

I am method1 in MyClass1
I am method2 in MyClass1
I am method1 in MyClass2
I am method2 in MyClass2

In the above example, we created an interface named MyInterface that defines two methods without any implementation (without a body). After that, we created two classes called MyClass1 and MyClass2 that implement the interface MyInterface with the keyword implements and define the body of both the methods, method1() and method2(), with different implementations in both the classes, MyClass1 and MyClass2.

Implement Multiple Interfaces in a Class

We can implement multiple interfaces in a class by listing them in the implements clause of the class definition, separated by commas.

Example

// Create an interface called MyInterface1 having two methods in it.
interface MyInterface1
{
    public void method1();
    public void method2();
}

// Create an interface called MyInterface2 having two methods in it.
interface MyInterface2
{
    public void method3();
    public void method4();
}

// Implement the interface MyInterface1 and MyInterface2 in a class called MyClass
class MyClass implements MyInterface1, MyInterface2
{
    public void method1()
    {
        // Implementation for method1
        System.out.println("I am method1 in MyClass");
    }
    public void method2()
    {
        // Implementation for method2
        System.out.println("I am method2 in MyClass");
    }
    public void method3()
    {
        // Implementation for method3
        System.out.println("I am method3 in MyClass");
    }
    public void method4()
    {
        // Implementation for method4
        System.out.println("I am method4 in MyClass");
    }
}

public class Example
{
    public static void main(String args[])
    {
        MyClass obj = new MyClass();
        obj.method1();
        obj.method2();
        obj.method3();
        obj.method4();
    }
}

Output

I am method1 in MyClass
I am method2 in MyClass
I am method3 in MyClass
I am method4 in MyClass

Extend an Interface with Multiple Interfaces

We can extend an interface with multiple interfaces by listing them in the extends clause of the interface definition, separated by commas. The interface that extends other interfaces is known as the child interface, and the interfaces it extends are known as parent interfaces.

Example

// Create an interface called MyInterface1 having one method in it.
interface MyInterface1
{
    public void method1();
}

// Create an interface called MyInterface2 having one method in it.
interface MyInterface2
{
    public void method2();
}

// Create an interface called MyInterface3 having one method in it
// and extends it with MyInterface1 and MyInterface2
interface MyInterface3 extends MyInterface1, MyInterface2
{
    public void method3();
}

// Implement the interface MyInterface3 in a class called MyClass
class MyClass implements MyInterface3
{
    public void method1()
    {
        // Implementation for method1
        System.out.println("I am method1 in MyClass");
    }
    public void method2()
    {
        // Implementation for method2
        System.out.println("I am method2 in MyClass");
    }
    public void method3()
    {
        // Implementation for method3
        System.out.println("I am method3 in MyClass");
    }
}

public class Example
{
    public static void main(String args[])
    {
        MyClass obj = new MyClass();
        obj.method1();
        obj.method2();
        obj.method3();
    }
}

Output

I am method1 in MyClass
I am method2 in MyClass
I am method3 in MyClass

In the above example, MyInterface3 is extending MyInterface1 and MyInterface2, which means it will inherit all the methods from both parent interfaces, and the implementing class will have to implement all the methods of all inherited interfaces as well.

In the above program, MyInterface3 is a child interface, and MyInterface1 and MyInterface2 are parent interfaces.