Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Why Should We Upcast Browser Driver Class Object To WebDriver?

Posted on 04/30/2024 By admin

In interview a frequently asked interview question is “Why do we upcast browser class object to WebDriver? or why we do not upcast to RemoteWebDriver class instead of WebDriver?” and we give all sort of answers.

First of all this question is asked with assumption that we do not have any option except upcast. Is it so? Absolutely not. It completely up to you whether you upcast or not and if you do then to which level? Question should be “Why should we upcast it or what are advantages if you upcast?

Before we start thinking this particular topic only with respect to Selenium WebDriver-Java, let’s learn core Java concepts i.e. Inheritance and upcast.

I define an Interface in Java as “A contract. I want something to do but I do not know how to do the same. I need a class or multiple classes to achieve that.“. This may not should exactly with purpose of Interface but at high level this we do.

“Amod” ( My name ) wants to perform swapping of two numbers in Java but he does not know the logic to do. So he has created an interface as below:-

package InheritanceExamples;

public interface WhatAmodWantsButDontKnowHowToDo {
        
        void swapTwoIntVariables(int a, int b);

}

Amod has a best friend “John” who has average programming knowledge and promises to give implementation of Amod’s method. Amod is so happy now. John gives implementation as below:-

package InheritanceExamples;

public class JohnKnowsImplementation implements WhatAmodWantsButDontKnowHowToDo{

        @Override
        public void swapTwoIntVariables(int a, int b) {
                System.out.println("--------------Before swap------------");
                System.out.println("a :" + a);
                System.out.println("b :" + b);
                /// Swap logic using temporary
                int c = a;
                a = b;
                b = c;
                System.out.println("--------------After swap------------");
                System.out.println("a :" + a);
                System.out.println("b :" + b);
        
        }

}

Now Amod creates a usage utility to run his incomplete method as below:-

package InheritanceExamples;

public class AmodUsesImplementationGivenByJohn {
        
        public static void main(String[] args) {
                
                JohnKnowsImplementation johnKnowsImplementation = new JohnKnowsImplementation();
                johnKnowsImplementation.swapTwoIntVariables(10, 20);
        }

}

Output :-

--------------Before swap------------
 a :10
 b :20
--------------After swap------------
 a :20
 b :10

Amod is happy now and everywhere he is appreciating John. One day he met his best mentor Mukesh who is a great developer. He says to Amod that he can provide a more optimal and memory efficient implementation of swapping method. Amod is more happy now and asked for the implementation from Mukesh. Mukesh gives implementation as below:-

package InheritanceExamples;

public class MukeshKnowsOptimalImplementation implements WhatAmodWantsButDontKnowHowToDo{

        @Override
        public void swapTwoIntVariables(int a, int b) {
                System.out.println("--------------Before swap------------");
                System.out.println("a :" + a);
                System.out.println("b :" + b);
                /// Swap logic without using temporary
                a = a+b;
                b = a -b;
                a = a-b;
                System.out.println("--------------After swap------------");
                System.out.println("a :" + a);
                System.out.println("b :" + b);
        
        }

}

Now Amod creates a new usage utility class so that he can use Mukesh’s implementation.

package InheritanceExamples;

public class AmodUsesImplementationGivenByMukesh {
        
        public static void main(String[] args) {
                
                MukeshKnowsOptimalImplementation mukeshKnowsOptimalImplementation = new MukeshKnowsOptimalImplementation();
                mukeshKnowsOptimalImplementation.swapTwoIntVariables(10, 20);
        }

}

As Amod is using Mukesh’s implementation it makes John sad. He says to Amod, he is not doing good with him. Amod does not want to make sad any one. He is in tension now what to do. He is also thinking if there is another new implementation given by someone else again he needs to create a new usage class and same problem may arise again.

He found a solution in core concept of Interface, Inheritance and runtime method overriding as below:-

package InheritanceExamples;

public class UpcastImplementation {
        
        public static void main(String[] args) {
                
                WhatAmodWantsButDontKnowHowToDo whatAmodWantsButDontKnowHowToDo;
                // Using John's Implementation
                whatAmodWantsButDontKnowHowToDo = new JohnKnowsImplementation();
                whatAmodWantsButDontKnowHowToDo.swapTwoIntVariables(10, 20);
                // Using Mukesh's Implementation
                whatAmodWantsButDontKnowHowToDo = new MukeshKnowsOptimalImplementation();
                whatAmodWantsButDontKnowHowToDo.swapTwoIntVariables(10, 20);
        }

}

Amod has created a reference of his Interface as whatAmodWantsButDontKnowHowToDo. Since both John and Mukesh’s implementation class implement Amod’s interface, whatAmodWantsButDontKnowHowToDo can hold any object of implemented class. If there is further new implementations of Amod’s method, he does not need to create a new class anymore. When John’s comes to meet Amod then he sees his implementation is being used and same for Mukesh. ( Please assume the game :-p Hypothetical statement ).

Story over but did you get the point of story?

Above classes in image are directly or indirectly implementations of WebDriver interface. In fact RemoteWebDriver is direct implemented class of WebDriver and remaining browser classes extends RemoteWebDriver. There is another update I can see in Selenium 4 that, a new class ChromiumDriver is introduced which extends RemoteWebDriver and ChromeDriver class extends ChromiumDriver now.

We know WebDriver is an interface and these implemented class complete it. Many implemented classes represent a browser. You can related WebDriver interface as WhatAmodWantsButDontKnowHowToDo interface and ChromeDriver , FirefoxDriver etc as MukeshKnowsOptimalImplementation and JohnKnowsImplementation.

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:

package InheritanceExamples; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver; public class WithOutUpcastToWebDriver { // Since user should be able to launch any browser , we need to create reference object of every browser type public static ChromeDriver cDriver; public static FirefoxDriver fDriver; public static InternetExplorerDriver iDriver; public static EdgeDriver eDriver; public static SafariDriver sDriver; // We need to keep adding browser driver reference variable
}




Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert
Next Post: Stream api in java

Related Posts

July 30, 2019 – Make Selenium Easy Uncategorized
fluent wait in selenium Uncategorized
Make Selenium Easy – Page 5 Uncategorized
Protractor Tutorial 4 – Introduction To Protractor Test Tool & How It Works Uncategorized
July 15, 2018 – Make Selenium Easy Uncategorized
TestNG Tutorials 36: Can a Test Method Return a Value in TestNG? – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark