Saturday, April 4, 2015

Interface in Java

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

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...