More About Interface in Java


The program demonstrates the use of interfaces in Java. It defines an interface interDemo with a static final variable A, two abstract methods fun1() and fun2(), a static method fun3(), a private method fun6(), and a default method fun5().

Then, the interface interDemo2 extends interDemo and adds another abstract method fun4(). The class TestInter implements the interface interDemo2 and provides implementations for all the abstract methods.

In the main() method, the static final variable A of interDemo is printed to the console using System.out.println(), and the static method fun3() of the same interface is called. This demonstrates the usage of static variables and methods in interfaces.

Overall, the program illustrates the use of interfaces in Java to define a set of methods that must be implemented by classes that implement the interface, and how static and default methods can also be defined in interfaces.

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