Understanding Interface concept of Java in Selenium – User friendly Tech help

When we first start with selenium automation our very first line of code comes as :-
nWebDriver driver = new FireFoxDriver;
n
Have we ever thought what is the meaning of this code of line …WebDriver driver ?
nSimple answer to this is “WebDriver is an Interface,and we are defining a reference variable(driver) whose type is an interface.Now any object we assign to it must be an instance of a class(FireFoxDriver) that implements the interface.”
n
nConfused!!!, no worries lets understand Interface concept in Java first, it would really assist us in clearing our doubts.
n
nDo follow us on FB,G+,Twitter or LinkedIn  to learn more 
n
nWhat is an Interface?
nInterface is like a blueprint of Class. It contains variables and body less methods(Abstract methods), where we just declare methods but we implement them inside the class which inherit Interface.
n
nNote:- Java 8 introduced “Default Method” or (Defender methods), which allows us to add new methods with declaration and definition (like a normal method)to the Interfaces without breaking the existing implementation of these Interface.
n
nExample:-

n

Public Interface WebDriver()n{

n

int iNum = 10;
npublic void get(String URL);

n

}

n

nAbove example just contain the structure of a get method, now a class which would implement this interface need to define the method.
n
nNote:- By default all the variables and methods inside an Interface are public-ally accessible. 
n
nExample:-

n

Public class FirefoxDriver implements WebDriver() { public void get(String sURL) { System.out.println("URL="+sURL);n}

n

}

n

nNow we know that incase of Interface, “If we define a reference variable whose type is an interface,any object we assign to it must be an instance of a class that implements the interface.”
n
nIn our example that Class is FireFoxDriver and Interface is WebDriver, so we can say WebDriver driver = New FirefoxDriver();
n

n

n

n

n

n

n

n

n

n

n
nNote:- We need to use the Implements Keyword to consume Interface inside a Class.

n

Lets learn more about an Interface
n
nInterface instance:-
nWe can create a reference variable of an interface but we can’t instantiate any interface since it is just a contract to be implemented in a Class.
n
nWebDriver driver = New WebDriver …Not allowed, 

nn

but below code is allowed….
Example:-
n
WebDriver driver = New FireFoxDriver();
ndriver.get(testUrl);
n
nCreating Interface in Eclipse:-
nLets create a sample interface in Eclipse and implement it inside a Class.
nRight click on package and go to -> New -> Interface.

nn

nImportant points:-

n

    n

  • n
      n

    1. Class that implements an interface must implement all the methods declared in the interface.Now our FirefoxDriver class should implement all the methods declared inside an WebDriver interface, same is the case with ChromeDriver or IEDriver classes.
    2. n

    3. While implementing methods, we must follow the exact same signature (name + parameters) as declared in the interface.
    4. n

    5. We can not instantiate/create object of an Interface. webdriver driver = new webdriver ();
    6. n

    7. All the variable inside an interface are by default Final.
    8. n

    9. Class cannot Extend Interface only Implements it.
    10. n

    11. Interface an Extend another Interface but then the class which implements the interface need to implemented the methods of both interface .
    12. n

    13. Class can implement multiple Interface(Remember class cannot extend multiple classes,multiple inheritance in class is not possible)
      n
      n

      n

      n

      n

      n

      n

      n

      n

    14. n

    15. Interface can not hold constructor.
    16. n

    17. Interface can not hold instance fields/variables.
    18. n

    19. By default all the methods of Interface are public so no need to provide access modifiers.
    20. n

    21. An interface can have another interface i.e. known as nested interface. 
    22. n

    n

  • n

n

Abstract Class v/s Interface 
nAbstract class is similar to interface, like we cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.But in abstract class, we can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that are  declared or defined (as default methods) are public. We can implement any number of interfaces unlike abstract class which can extend only one class.
n
n
nNote:- We could have implemented WebDriver driver = New FirefoxDriver(); as FirefoxDriver driver = new Firefoxdriver();
n
nBut we would face problem incase we need to run the same code for chromedriver then we need to create the object as ChromeDriver driver = new ChromeDriver();.
n
nThus to avoid this we are creating reference of WebDriver and assigning the object of implementing class i.e webdriver driver; driver = new firefoxdriver or driver = new chromedriver();
n
n

Was this article helpful?
YesNo

Similar Posts