Tuesday, 14 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.: WebDriver wd= 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

public interface phone {
          public void call();
          public void reject();

}


public class mod1 implements phone {
          public void call() {
                   System.out.println("logic for mod1 call");      
                   }
          public void reject() {
                   System.out.println("logic for mod1 reject");
                   }
          public void dict() {
                   System.out.println("logic for dist ");
                   }


}
public class mod2 implements phone {
          public void call() {
                   System.out.println("logic for mod1 call");      
                   }
          public void reject() {
                   System.out.println("logic for mod1 reject");
                   }
          public void dict() {
                   System.out.println("logic for dist ");
                   }


}



public class test {

         
         


public static void 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