3

I have started learning Java and selenium and I would like to understand the difference between the below two set of code, and both of them executed fine and have the same behavior.

WebDriver is an interface and it is implemented by a class FirefoxDriver which has implemented all its methods. Hence to invoke methods present in FirefoxDriver we are creating a firefox object with it's constructor but why often people use first one (in below set) and what does it means when we use a constructor of a the class for creating the object but instead of that class name we put the interface name(which the class has implemented).

// First One

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FirstOne
{
public static void main(String[] args)
{
           WebDriver driver = new FirefoxDriver();  
       driver.get("http://google.com");
           System.out.println(driver.getTitle());
           driver.close();
}
} 

// Second one

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FirstOne
{
public static void main(String[] args)
{

       FirefoxDriver driver = new FirefoxDriver();
           driver.get("http://google.com");
           System.out.println(driver.getTitle());
           driver.close();
}
} 
user1925406
  • 713
  • 3
  • 15
  • 33

3 Answers3

3

Since FirefoxDriver implements the WebDriver interface, you can use references of type WebDriver to point to instances of FirefoxDriver. This is basically done to follow an OOP principle which says -

Program to an interface, not to a concrete implementation.

Using the first approach gives you a lots of flexibility, it's kind of a best practice followed by the programmers. Referring to objects by their interfaces enable you to change the implementation later without affecting much of your existing code. This approach is particularly useful for something like Dependency Injection.

For more information, please check out this answer. You can also check out Effective Java, Item 52 -

Refer to objects by their interfaces.

Community
  • 1
  • 1
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
3

Sayem is right but let me expand a bit on WHY this is done.

This allows what we call decoupling. Essentially the code that is using the WebDriver only cares that it is a webdriver, not the implementation of web driver. In essence, the calling code should only care about WHAT a WebDriver does, not HOW.

This notion of decoupling, or "separation of concerns" is a fundamental aspect of OO. Google can pull up tons of articles on the subject and it's really worth reading as it's an underpinning of good java and good OO programming.

Taylor
  • 3,942
  • 2
  • 20
  • 33
0

As a practical example applying the principals of Object Oriented design, is that you if program to an interface, then the concrete implementation can be swapped out.

If you have:

FirefoxDriver driver = new FirefoxDriver();

What happens if someone asks you to run this test on Chrome, or Internet Explorer? If you program to the interface, you could simply do this:

WebDriver driver = new FirefoxDriver();

or

WebDriver driver = new ChromeDriver();

or

WebDriver driver = new InternetExplorerDriver();

This way, you're not stuck or limited to always implementing the FirefoxDriver, you can swap it out as needed with other concrete implementations of the interface.

Nathan Dace
  • 1,545
  • 9
  • 21