0

We can create a driver object using the following ways.

WebDriver driver = new FirefoxDriver();

FirefoxDriver driver = new FirefoxDriver();

FirefoxDriver is a class that is implementing WebDriver Interface.

In the first statement we are using the interface name and in the second one we are using class name. Is there any difference between the two like we can't access some methods etc.?

I looked for this question on stackoverflow and found that first one is dynamic binding and second one is static binding but is there any real difference between the two declarations above?

If we don't have any difference why we should have two declarations?

Can someone answer this please?

Thank you.

Subbu
  • 217
  • 2
  • 11
  • Can you please give me links to those posts where it is explained? I tried to search them but got posts explaining differences like static and dynamic but not exactly the difference? I would like to check other posts suggested by you. – Subbu Sep 21 '18 at 12:04
  • 1
    Thank you Sotirios Delimanolis and Raj. Both of you guys made my day. I had this question for a long time and it's answered finally. I think I haven't used the right keywords to search here like "Program to an interface". But anyhow, thank you very much both of you. – Subbu Sep 21 '18 at 12:11

1 Answers1

1

That is a basic Design Principle : "Program to an interface rather than to an implementation". In very simple terms, this helps us to change the data structures easily in future without much modifications.

Suppose today, you use one implementation of a list, let's say ArrayList. Tomorrow, you may realize that you need to use LinkedList. If you had programmed to an interface, it's just one line change, because everywhere in your code you are using the interface reference variable. On the other hand, if you had programmed to an implementation, you need to change all the references to that variable. That may involve changing the method return types, method arguments, getters, setters etc.

we need a type that can be used instead of both FirefoxDriver and ChromeDriver. Hence WebDriver driver = new FirefoxDriver() gives you flexibility for same.

Reference : https://seleniumjava.com/2016/07/13/how-does-webdriver-driver-new-firefoxdriver-work/

Raj
  • 707
  • 6
  • 23