Monday, 13 July 2015

INTERFACE CONCEPT IN JAVA WITH EXAMPLE



 Interface:
It is a set of rules defined by us which has only declaration of functions, no body of the function and it will force us to override all the functions in the class which implements this interface.

Syntax: [modifier] interface InterfaceName {
          [Modifier] [Return type] methodName1();
[Modifier] [Return type] methodName2();
                   ….. }
Note:
  One can implement one or more interfaces at a time.
We can’t create an object for an interface.
We can create an object for a class which is implementing the interface and then give the reference of the interface.
Syntax: interface InterfaceName =new ClassName()
E.g.: WebDriverwd= new FireFoxDriver();
By declaring like this one can access all the WebDriver’s stub as well as only overridden stub from ClassName FireFoxDriver





Develop a java program which describes interface concept

publicinterface phone {
          publicvoid call();
          publicvoid reject();

}


publicclass mod1 implements phone {
          publicvoid call() {
                   System.out.println("logic for mod1 call");      
                   }
          publicvoid reject() {
                   System.out.println("logic for mod1 reject");
                   }
          publicvoiddict() {
                   System.out.println("logic for dist ");
                   }


}
publicclass mod2 implements phone {
          publicvoid call() {
                   System.out.println("logic for mod1 call");      
                   }
          publicvoid reject() {
                   System.out.println("logic for mod1 reject");
                   }
          publicvoiddict() {
                   System.out.println("logic for dist ");
                   }


}



publicclass test {

         
         


publicstaticvoid main(String[] args) {
                   phone p1=new mod1();
                   // phone p2=new mod2();
                                      p1.call();
                                      p1.reject();
                                      p2.call();
                                      p2.reject();
                                      //p1.dict();
                                      //p2.dict();
                             }


}


No comments:

Post a Comment