Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

Interface

• In Java an Interface is a User-Defined data type.
• Using interface keyword, we can declare an Interface.

Syntax: -interface I1{ }

Here, interface is a keyword and I1 is an interface name.

• Like in classes, we get a .class file for an interface after its successful compilation.
• In an interface we generally declare two different types of members which are Variable and Method.
• When we declare a variable in an interface then it will always be public static final.
• When we declare a method inside an interface then by default it will be public abstract.

            
interface I1{
	int a = 10; // public static final int a = 10;
}  
Class JTC{
	public static void main(String arg[]){
		System.out.println(I1.a);
		// I1.a = 1000; error: - cannot assign a value into a final variable
    }
}
        
    10
            

Here, in this example we have an interface I1 which contains int a = 10; as we know that when we declare any variable into an interface it can be only public static final, so int a = 10 is public static final int a = 10.

In the above example we have an interface I1 and inside the I1 interface we have declared a variable int a = 10 (public static final int a = 10), that is the reason from JTC class we are able to access the value of a along with the interface name. In the next line when we try to assign 1000 into int a, we get an error because int a is a final type of variable.

            
interface I1{
	// void m1(){} :- error : interface abstract method cannot have body.
	void m2();
}
        

In the above example we have an interface I1. Here ,as we can see, we are declaring a method along with the body. However, in this line of code we get an error; because as we discussed, an interface method is an abstract method by default. In the next line we have an error free declaration of method which is void m2 () and it is equivalent to public abstract void m2 (), so we need to create a sub class of I1 interface then only we can override void m2 ().

• Unlike as in class, we cannot create a sub-class of an interface using extends keyword.
• In Java we have a dedicated keyword implements that can be used to create a sub-class of an interface.
• When we use a class to implement an interface then we have to override all the abstract methods of super interface into the sub class, alternatively we can declare that sub-class as an abstract class.

            
class JTC{
	public static void main(String arg[]){
		I1 i1 = new B();
		int a = i1.m1();
		i1.m2();
		System.out.println("a :- "+a);
	}
}
interface I1{
	int m1();
	void m2();
}

abstract class A implements I1{
	public int m1(){
		System.out.println("m1 in A class");
		return 10;
	}
}

class B extends A{
	public void m2(){
		System.out.println("m2 in A class");
	}
}
        
    m1 in A class
    m2 in A class
    a :- 10
            

In this example we have an interface I1 with two abstract methods int m1 () and void m2 () and class A is implementing I1 interface; but we are not overriding void m2 () method in A class, that is the reason class A is an abstract class. In the next section of the example, we have a class B which is extending class A; as we can see in class B we are overriding the void m2 () which is not overridden in A class, that is the reason there is no need to declare class B as an abstract class.

• Using interface, we can easily achieve Multiple Inheritance.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		a1.m1();
	}
}
interface I1{
	int a = 10;
}
interface I2{
	int b = 20;
}
interface I3{
	int c = 30;
} 
class A implements I1,I2,I3{
	void m1(){
		System.out.println("m1 in A class");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
		System.out.println("c :- "+c);
	}
}
        
    m1 in A class
    a :- 10
    b :- 20
    c :- 30
            

This example helps us to understand how we can achieve Multiple Inheritance using interface. In this example we have three interfaces which are I1, I2 and I3 and class A is implementing all interfaces; it means I1, I2 and I3 all are the direct super interfaces of class A, that is the reason we are able to access the value of a, b and c into m1() of A class directly.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		a1.m1();
	}
}
interface I1{
	int a = 10;
}
interface I2{
	int a = 20;
}
inetraface I3{
	int a = 30;
} 
class A implements I1,I2,I3{
	void m1(){
		System.out.println("m1 in A class");
		// System.out.println("a :- "+a);
		System.out.println("I1.a :- "+I1.a);
		System.out.println("I1.a :- "+I1.a);
		System.out.println("I1.a :- "+I1.a);
	}
}
        
    m1 in A class
    I1.a :- 10
    I1.b :- 10
    I1.c :- 10
            

In this example we will understand how we can address the ambiguity problem in multiple inheritance using interfaces. Here we can see that in I1, I2 and I3 interfaces we have variables with the same name, when we try to access the value of a in class A method then JVM is unable to carry out the instruction because in all the interfaces we have variables with same name. That is the reason when we access the int a from m1() of class A directly then we get an error at compile time; so to access the value of int a from the m1() we should use the specific interface name, as we can see in the above example.

• An interface can also inherit other interfaces using extends keyword.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		a1.m1();
	}
}
interface I1{
	int a = 10;
}
interface I2{
	int b = 20;
}
interface I3 extends I1,I2{
	int c = 30
}
class A implements I3{
	void m1(){
		System.out.println("m1 in A class");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
		System.out.println("c :- "+c);
	}
}
        
    m1 in A class
    I1.a :- 10
    I1.b :- 10
    I1.c :- 10
            

In the above example we can see that I3 interface is inheriting I1 and I2 interfaces using extends keyword and class A is implementing I3 interface; so all the functionalities of I3 interface are directly contained in class A, also the functionalities of I1 and I2 interfaces are indirectly contained in class A.

Note: - From JDK 1.8, we can define method with the help of body as well as by using default and static keyword.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		a1.m1();
		int i = I1.m2();
		System.out.println("i :- "+i);
	}
}
    interface I1{
	default void m1(){
		System.out.println("m1 in I1 interface");
	}
	static int m2(){
		System.out.println("m2 in I1 interface");
		return 10;
	}
}

class A implements I1{
}
        
    m1 in I1 interface
    m2 in I1 interface
    i :- 10