ads

Saturday, February 13, 2016

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 which implements an interface must define the body of method(s) of interface(s). Final constants can't be modified once they are initialized; final, interface, extend and implements are Java keywords.
Declaration of interface:
interface InterfaceName {
// constants declaration
// methods declaration
}

Interface program in Java

In our program we create an interface named Info which contains a constant and a method declaration. We create a class which 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 interface
 
public void display() {
System.out.println(language + " is awesome");
}
}
Download Interface program class file.
Output of program:
Interface java example program output

No comments:

Post a Comment