What is Typecasting? Upcasting vs Downcasting

Typecasting is the process of converting one data type to another.

There are two types of object typecasting in Java:

  • Upcasting
  • Downcasting

Upcasting

Upcasting is a type of object typecasting in which a child class object is assigned to a parent class reference.

Advertisement

Using upcasting, we can access the methods and variables available in the parent class or interface.

Example

 
class Animal {

    void sound() {
        System.out.println("Animal Sound");
    }
}

class Dog extends Animal {

    void bark() {
        System.out.println("Dog Barking");
    }
}

public class Demo {

    public static void main(String[] args) {

        Animal obj = new Dog();   // Upcasting

        obj.sound();
    }
}
 

Here,

  • Dog → Child class
  • Animal → Parent class
  • Animal obj = new Dog(); → Upcasting

Downcasting

Downcasting is the reverse of upcasting.

Here, a parent reference is converted back into a child reference.

If the object is not actually an instance of the child class, Java throws a ClassCastException at runtime.

Example

 
Animal obj = new Dog();      // Upcasting

Dog d = (Dog) obj;           // Downcasting

d.bark();
 

Invalid Downcasting

 
Animal obj = new Animal();

Dog d = (Dog) obj;
 

Output

 
ClassCastException
 

Because an Animal object cannot become a Dog.


Upcasting vs Downcasting

Feature Upcasting Downcasting
Conversion Child → Parent Parent → Child
Casting Required No (Implicit) Yes (Explicit)
Runtime Exception No May throw ClassCastException
Safe Yes Only if object is actually child type

Where Is Upcasting Used in Selenium?

The most famous Selenium statement is itself an example of upcasting.

 
WebDriver driver = new FirefoxDriver();
 

Breaking this statement down:

  • WebDriver → Interface
  • driver → Reference variable
  • = → Assignment operator
  • new → Creates an object
  • FirefoxDriver() → Constructor

Here,

  • FirefoxDriver is the child class.
  • WebDriver is the parent interface.

So,

 
WebDriver driver = new FirefoxDriver();
 

is an example of upcasting.

This is what makes Selenium browser-independent.

To switch browsers, only one line changes.

Chrome

 
WebDriver driver = new ChromeDriver();
 

Firefox

 
WebDriver driver = new FirefoxDriver();
 

Edge

 
WebDriver driver = new EdgeDriver();
 

The remaining automation code stays exactly the same.


Typecasting with TakesScreenshot

Screenshots are captured using the TakesScreenshot interface.

The screenshot method belongs to this interface.

 
getScreenshotAs()
 

Since WebDriver doesn't directly expose this method, we typecast the driver.

 
TakesScreenshot ts = (TakesScreenshot) driver;

File srcFile = ts.getScreenshotAs(OutputType.FILE);
 

or

 
File srcFile =
((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
 

OutputType.FILE returns the screenshot as a file.

Initially, Selenium stores the screenshot inside a temporary folder.

Normally, we copy it to a permanent location.

 
FileUtils.copyFile(srcFile,
new File("C:\\Screenshots\\HomePage.png"));
 

Typecasting with JavascriptExecutor

JavascriptExecutor is another Selenium interface.

It allows Selenium to execute JavaScript inside the browser.

It provides two methods.

 
executeScript()

executeAsyncScript()
 

Since WebDriver doesn't directly expose these methods, we typecast.

 
JavascriptExecutor js =
(JavascriptExecutor) driver;
 

Now JavaScript can be executed.

 
js.executeScript("alert('Hello')");
 

Uses of JavascriptExecutor

JavascriptExecutor is commonly used for:

  • Scrolling web pages
  • Clicking hidden elements
  • Handling disabled elements
  • Setting hidden field values
  • Executing custom JavaScript
  • Handling complex React or Angular applications
  • As a fallback when Selenium methods fail

Scroll Down

 
JavascriptExecutor js =
(JavascriptExecutor) driver;

js.executeScript("window.scrollTo(0,1000);");
 

Scroll to a Specific Element

 
WebElement element =
driver.findElement(By.id("elementId"));

js.executeScript(
"arguments[0].scrollIntoView(true);",
element);
 

Scroll to a Fixed Position

 
js.executeScript("window.scrollTo(0,500);");
 

Horizontal Scrolling

 
js.executeScript("window.scrollTo(500,0);");
 

JavaScript Click

Sometimes Selenium's normal click() fails because the element is hidden, overlapped, or not clickable.

Use JavaScript.

 
js.executeScript(
"arguments[0].click();",
element);
 

This is the standard fallback solution.


Real-Time Interview Example (STAR)

Situation

The application had dynamically loaded content.

The Submit button was sometimes hidden.

Task

Click the Submit button consistently.

Action

Used JavascriptExecutor to:

  • Scroll to the button.
  • Perform a JavaScript click.

Result

  • Reduced flaky test failures.
  • Improved automation stability.
  • Worked consistently across different environments.

Best Practice

Use JavascriptExecutor only when necessary.

Prefer Selenium's built-in methods first:

  • click()
  • sendKeys()
  • clear()

Use JavaScript only if those methods fail.


FAQs

1. What is typecasting?

Typecasting is the process of converting one data type into another.

There are two types:

  • Upcasting
  • Downcasting

2. What is upcasting?

Assigning a child class object to a parent class reference.

Example:

 
WebDriver driver = new ChromeDriver();
 

3. What is downcasting?

Converting a parent reference back into a child reference.

Example:

 
Dog d = (Dog) obj;
 

It may throw a ClassCastException if the object is not actually of the child type.


4. Where is upcasting used in Selenium?

 
WebDriver driver = new ChromeDriver();
 

or

 
WebDriver driver = new FirefoxDriver();
 

Both are examples of upcasting.


5. Why do we typecast for screenshots?

Because getScreenshotAs() belongs to the TakesScreenshot interface.

 
File file =
((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
 

6. What is JavascriptExecutor?

An interface used to execute JavaScript inside the browser.

It provides two methods:

  • executeScript()
  • executeAsyncScript()

7. What are the uses of JavascriptExecutor?

  • Scrolling pages
  • Clicking hidden elements
  • Handling disabled elements
  • Reading hidden fields
  • Executing custom JavaScript
  • Working with dynamic web applications

8. How do you scroll in Selenium?

Scroll by pixel

 
js.executeScript("window.scrollTo(0,1000);");
 

Scroll to an element

 
js.executeScript(
"arguments[0].scrollIntoView(true);",
element);
 

No.

Use Selenium's standard methods (click(), sendKeys(), clear()) whenever possible.

Use JavascriptExecutor only when standard WebDriver methods fail.