Understanding Interface concept of Java in Selenium

When we first start with selenium automation our very first line of code comes as :-
WebDriver driver = new FireFoxDriver;

Have we ever thought what is the meaning of this code of line …WebDriver driver ?
Simple 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.”

Confused!!!, no worries lets understand Interface concept in Java first, it would really assist us in clearing our doubts.

Do follow us on FB,G+,Twitter or LinkedIn  to learn more 

What is an Interface?
Interface 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.

Note:- 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.

Example:-

Public Interface WebDriver()
{

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

}


Above example just contain the structure of a get method, now a class which would implement this interface need to define the method.

Note:- By default all the variables and methods inside an Interface are public-ally accessible. 

Example:-

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

}


Now 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.”

In our example that Class is FireFoxDriver and Interface is WebDriver, so we can say WebDriver driver = New FirefoxDriver();


WebDriver interface implements Firefoxdriver class



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

Lets learn more about an Interface

Interface instance:-
We 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.

WebDriver driver = New WebDriver …Not allowed, 

cannot instantiate interface in java

but below code is allowed….
Example:-

WebDriver driver = New FireFoxDriver();
driver.get(testUrl);

Creating Interface in Eclipse:-
Lets create a sample interface in Eclipse and implement it inside a Class.
Right click on package and go to -> New -> Interface.

Creating Interface in Eclipse


Important points:-

    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. While implementing methods, we must follow the exact same signature (name + parameters) as declared in the interface.
    3. We can not instantiate/create object of an Interfacewebdriver driver = new webdriver ();
    4. All the variable inside an interface are by default Final.
    5. Class cannot Extend Interface only Implements it.
    6. Interface an Extend another Interface but then the class which implements the interface need to implemented the methods of both interface .
    7. Class can implement multiple Interface(Remember class cannot extend multiple classes,multiple inheritance in class is not possible)
      Class inherit from multiple Interface

    8. Interface can not hold constructor.
    9. Interface can not hold instance fields/variables.
    10. By default all the methods of Interface are public so no need to provide access modifiers.
    11. An interface can have another interface i.e. known as nested interface. 

Abstract Class v/s Interface 
Abstract 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.


Note:- We could have implemented WebDriver driver = New FirefoxDriver(); as FirefoxDriver driver = new Firefoxdriver();

But 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();.

Thus 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();


47 Replies to “Understanding Interface concept of Java in Selenium”

  1. can i know how to call the same webdriver that is declared in one class should be called in all the classes in my package.
    ex:class login{
    Webdriver d;
    }
    class logout{
    the same webdriver instance should be created here also.
    Webdriver d;
    }
    thanks in advance…..

  2. you can pass it as parameter in the methods you want to use it.

    Like :
    public void getWebDriver( Webdriver driver )

  3. Because the methods implementation declared inside WebDriver interface differs as per requirement.
    As mentioned above.
    webdriver driver; driver = new firefoxdriver or driver = new chromedriver();

  4. i Read/see many blogs youtube vedio to understand why and where we use interface..the examples given here really a eye opener for me.thank you so much to describe such a big thing in a simpler way

  5. It will work no doubt.But to define a generic structure for all driver instance the WebDriver is declared as an Interface so that we can use the same Interface object for chrome,firefox,ie,safari..or any web browser come in future.

  6. because if you use WebDriver driver = new FireFoxDriver(), then you can use same "driver" object for initialising Chrome driver or IE driver (i.e. driver = new ChromeDriver()), otherwise you need to keep on creating driver objects for each browser

  7. Because WebDriver interface is just a blue print it just tells you what method your derive class should contain, Now If you make it abstract class then you need to define complete body of method which has no meaning as it will never going to be in use it will always be overridden by derive class methods.

Leave a Reply

Your email address will not be published. Required fields are marked *