Interface in Java

Created with Sketch.

 

Interface in Java

Interface in Java: Java interfaces are like Java classes, but they contain only static final constants and declaration of methods. Methods are not defined, and classes that implement an interface must define the body of the method(s) of the interface(s). Final constants can’t change after we initialize them. The final, interface, extend, and implements are keywords of Java.

Declaration of interface:

interface InterfaceName {
// constants declaration
// methods declaration
}

Interface program in Java

In our program, we create an interface named Info that contains a constant and a method declaration. We create a class that implements this interface by defining the method declared inside it.

interface Info {
static final String language = “Java”;
public void display();
}

class Simple implements Info {
public static void main(String []args) {
Simple obj = new Simple();
obj.display();
}

// Defining method declared in the interface

public void display() {
System.out.println(language + ” is awesome”);
}
}

 

Output of program:
Interface java example program output

Leave a Reply

Your email address will not be published. Required fields are marked *