When we are using

WebDriver driver = new ChromeDriver();

driver object will show methods of only WebDriver interface or ChromeDriver methods plus extra methods created in RC, Chromium or Chrome?

To understand I did some homework let me know where I went wrong.

Inside Package

  1. Interface - SuperInterface
  2. Interface - ChildSuperInterface extends SuperInterface
  3. Class - SuperClass implements ChildSuperInterface
  4. Class - ChildSuperClass extends SuperClass

I created some extra methods in ChildSuperclass

Now When I have created a separate class name WorkOnClass and create object driver1 in it. driver1 shows only methods of interface ChildSuperInterface. Not showing extra methods created in parent or child class SuperClass and ChildSuperClass.

ChildSuperInterface driver1 = new ChildSuperClass();

Need more understanding of this if you can help. Thanks in advance.

  • This seems to be a basic OOP question, not actually specific to QA or testing at all. – jonrsharpe Jun 9 at 13:59

ChromeDriver is an implementation of WebDriver interface, thus it must implement all the methods defined in the interface. If you instantiate a WebDriver object using WebDriver interface you get access to the methods defined in the interface only.

This is a basic example of composition.

| improve this answer | |

When you define your driver as

WebDriver driver = new ChromeDriver();

using reference driveryou will be able to access only the methods which are defined in WebDriver and implemented in ChromeDriver.

Basically, in Java you can access the objects through the references of parent types because those child objects inherit what is publicly accessible in parents. But you will not be able to access parent object using reference of child type because parent objects do not have methods descibed in child types.

You will still be able to access ChromeDriver specific methods if you cast your driver. For example:

| improve this answer | |
  • If you find yourself using ((ChromeDriver)driver) you should just convert the initial definition to ChromeDriver driver, otherwise it's too easy to later on switch that definition and not find out you just broke something until runtime. – jonrsharpe Jun 9 at 15:25
  • True but not completely. You might not be aware of what particular implementation is under your WebDriver. Your framework might support many. So it might happen that somewhere in your framework you test your instance for if it is ChromeDriver or FirefoxDriver and then invoke some specific method on that. – Alexey R. Jun 9 at 15:39
  • I don't think we disagree, so perhaps I didn't express myself clearly. My point is that if you have WebDriver driver = new ChromeDriver() and later on ((ChromeDriver)driver).specificMethod(), then if you change the initial definition to e.g. WebDriver driver = new FirefoxDriver() the compiler can't tell you .specificMethod will be a NPE. Instead if you have ChromeDriver driver = new ChromeDriver() and driver.specificMethod(), then if you change the definition to FirefoxDriver driver = new FirefoxDriver() (or even WebDriver driver = ), the compiler errors on .specificMethod. – jonrsharpe Jun 9 at 15:48
  • You're right that if you explicitly test then cast, that's OK, but that detail isn't in the answer. – jonrsharpe Jun 9 at 15:48
ChildSuperInterface driver1 = new ChildSuperClass();

Tells that driver1 is of type ChildSuperInterface , so it will have only the methods in the class : Interface - ChildSuperInterface extends SuperInterface.

it won't have the inherited methods, from SuperInterface

it won't have methods from any other subclasses as its not inherited yet when seen from ChildSuperInterface

If you want all the methods then you should give the type as SuperClass

SuperClass driver1 = new ChildSuperClass();

in this case it will have all the child methods and inherited methods

You should check access modifiers also:

enter image description here

In case of webdriver:

Webdriver looks like

chromedriver implements webdriver

so if you mentions

chromedriver driver; 

It will have only methods from chromedriver class and no methods from webdriver interface.

webdriver driver; 

will have both chromedriver and webdriver methods

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.