More About Interface in Java


Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signatures, no body, see: Java abstract method). A class can extend the features of other classes by use of inheritance. When a class extending more than one class is known as multiple inheritance. But Java doesn’t allow it because it creates the diamond problem and too complex to manage. We can achieve the multiple inheritances by use of interface.

Source Code

interface interDemo {
    final static int A = 25;
 
    public abstract void fun1();
 
    public abstract void fun2();
 
    public static void fun3() {
        System.out.println("I am Fun-3");
    }
    private void fun6(){
        System.out.println("Fun-6");
    }
    default void fun5() {
        System.out.println("Fun-5");
    }
}
 
interface interDemo2 extends interDemo {
    void fun4();
}
 
class TestInter implements interDemo2 {
 
    @Override
    public void fun1() {
        System.out.println("Fun-1");
    }
 
    @Override
    public void fun2() {
        System.out.println("Fun-2");
    }
 
    @Override
    public void fun4() {
        System.out.println("Fun-4");
    }
}
 
public class interfaceModeDemo {
    public static void main(String[] args) {
        System.out.println("A : " + interDemo.A);
        interDemo.fun3();
    }
}
 

Output

A : 25
I am Fun-3
To download raw file Click Here

Basic Programs