Why Do We Up cast Browser Driver Class Object To WebDriver?

Hello Folks,

In this post, we will learn about a frequently asked interview question in selenium that “Why we upcast a browser driver class object to WebDriver type?”.


Why do we write as below:

WebDriver driver= new FirefoxDriver();
WebDriver driver= new ChromeDriver ();

Why not as below:

FirefoxDriver driver= new FirefoxDriver();
ChromeDriver driver= new ChromeDriver ();

Let’s discuss about this.

First of all, all above statements are correct. It is not mandatory to up cast a browser driver class object to WebDriver or any higher class/interface in hierarchy.

Now question is if it is not mandatory, why we do so?

We should be aware hierarchy of Selenium webdriver classes and interfaces. You can learn about it from here.

WebDriver Interface is super interface (Not super most) of every browser driver classes like ChromeDriver , FirefoxDriver etc. And as per java concept, a super class/interface reference can hold a its sub class object but vice versa is not possible.

We will see two approaches here:

  1. When we do not upcast to WebDriver
  2. When we upcast to WebDriver

When we do not upcast to WebDriver:

Suppose, you need to create a base class which need to be extended in every class and perform some actions. User should be able to launch any browser.

You will create a base class as below:

You will create test scripts as below:


  • In the same style you need to create other browser driver scripts and use proper reference variable of browser type.
  • In this approach, if a new browser comes in market, you need to change Base class because we need to define a new reference variable of type new browser class.
  • Modification is not easy because it require changes at framework level.
  • You can not use methods defined in above classes in hierarchy.
  • It will problem to maintain a same reference of browser driver. You might get null pointer exception.

When we upcast to WebDriver:

We will create a very simple base class as below:

We will create scripts as below:

In this approach, you can use same reference variable of type WebDriver to launch any browser.


  • If you compare both approach, you will understand that we upcast for better architecture , best coding practice, ease of modification and enhancements. There is no hard core rule that we need to do it.
  • We can launch any browser using same reference type.
  • If new browser comes in market, there is no need to do any changes in framework (Base class).
  • You can use methods of classes in hierarchy which requires downcasting. For ex: To take screenshot and execute java script.
  • We achieve run time polymorphism here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning