Interface in Java

HOME

In this tutorial, I’ll explain about Interfaces in Java.

What is Interface?

An interface is a group of related methods with empty bodies (abstract methods).

  1. All the methods in Interface are public and abstract. 
  2. A class can implement more than one interface.
  3. An interface can extends another interface or interfaces 
  4. A class that implements interface must implements all the methods in interface. To implement interface use implements keyword.
  5. The variables declared in an interface are public, static & final by default.
  6. Java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .

Syntax of Interface

interface test {
//declare methods which are abstract by default
//declare variables which are public, static, final by default
}

Relationship between Class and Interfaces

Below is an example of implementation of Interface. I have declared a class as Interface which have two abstract methods.

public interface MyInterface {

	// Abstract methods
	public void method1();

	public void method2();
}

Interface Implementation

public class InterfaceTest implements MyInterface {

	public void method1() {
		System.out.println("Implementation of Method 1");
	}

	public void method2() {
		System.out.println("Implementation of Method 2");
	}

	public static void main(String[] args) {
		MyInterface interTest = new InterfaceTest();
		interTest.method1();
		interTest.method2();

	}
}

Output

Implementation of Method 1
Implementation of Method 2

Multiple Interface

In the below example, Interface 2 extends Interface 1 and class implements Interface 2.

Interface 1

public interface Infa1 {
	public void method1();

}

Interface 2

public interface Infa2 extends Infa1 {
	public void method2();

}

MultipleInterfaceTest Class

public class MultipleInterfaceTest implements Infa2 {

	public void method1() {
		System.out.println("Implementation of Method 1");
	}

	public void method2() {
		System.out.println("Implementation of Method 2");
	}

	public static void main(String[] args) {

		Infa2 obj = new MultipleInterfaceTest();
		obj.method1();
		obj.method2();
	}

}

Output

Implementation of Method 1
Implementation of Method 2

Multiple Inheritance

Interface supports multiple Inheritance. Here, there are two interfaces which are implemented in class MultipleInheritanceTest.

Interface 1

public interface Infa1 {
	public void method();

}

Interface 2

public interface Infa2 {
	public void method();

}

MultipleInheritanceTest Class

public class MultipleInheritanceTest implements Infa1, Infa2 {

	public void method() {
		System.out.println("Implementation of Method ");
	}


	public static void main(String[] args) {
		MultipleInheritanceTest test = new MultipleInheritanceTest();
		test.method();
	}

}

Output

Implementation of Method

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s