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 moreWhat 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;publicvoidget(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:-

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

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

Lets learn more about an InterfaceInterface 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,

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.

Important points:-

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.

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

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

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

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

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…..

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.

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

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.