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();
![]() |
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,
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 ();
- All the variable inside an interface are by default Final.
- Class cannot Extend Interface only Implements it.
- Interface an Extend another Interface but then the class which implements the interface need to implemented the methods of both interface .
- Class can implement multiple Interface(Remember class cannot extend multiple classes,multiple inheritance in class is not possible)
- Interface can not hold constructor.
- Interface can not hold instance fields/variables.
- 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();
47 replies on “Understanding Interface concept of Java in Selenium”
Really a good one …informative..Get to know the reason of driver object instance
Very nice explanation, its so simple to understand. Thanks for the author.
very nice
Nicely explained!
Thanks alot…its enough for beginner..!!!
very nice
Why do you think, developer has made the WebDriver an Interface instead of Abstract Class?
Very Good Explanation…Most tricky and common question in interview.
wow! what a explanation! awesome it is…. thank you so much
Thank you very much! You explain the concept very well. Keep it up. Thanks for the post.
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…..
Is it possible assign object of user defined class..
WebDriver ref=new MyClass();
Very Nice explanation… Great work done indeed.
it was very helpful. Thankyou so much. keep it up with the great work.
Perfect!!!!!
Very Nice..
you can pass it as parameter in the methods you want to use it.
Like :
public void getWebDriver( Webdriver driver )
Because the methods implementation declared inside WebDriver interface differs as per requirement.
As mentioned above.
webdriver driver; driver = new firefoxdriver or driver = new chromedriver();
hello,
the best explanation i ever came across…thank you very much.
Very clearly explained
thank you, was very useful.
The most awesome article explaining Interface in the most simplest language.
Thanks a lot.
Thanks alot. It was very lucidly explained.
if it is the case, without "implements" keyword how webDriver can be implemented?
Very well explained. Really helpful.
Great
Very nice….
excellent!!!
Your explanation is awesome.
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
why we need to assign object to the reference variable of interface type… what problem occurs if we create as FirefoxDriver driver=new FirefixDriver();
Thanks very good explanation.
simple in words but most informative article i have come across,looking forward for more such explanations
Very well explained
Thank you such a simple and clear explaination
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.
it was very very helpful. Thank you. keep it up with the good work.
Very nice 🙂
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
Unless your class inherit WebDriver and implement all the methods mention in WebDriver Interface
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.
Thank you for your time and explanation
very helpful. awesome
very nicely explained.. thanks
Nice article 🙂
Nicely explained. Thanks a lot!
Very Good explanation which is understandable for all.