Method Overloading Vs Method Overriding - automation testing

Method Overloading Vs Method Overriding - automation testing

Introduction

Overriding and overloading are the core concepts in Java programming. They are the ways to implement polymorphism in our Java programs.


1. Overloading in Java

  • Definition: Overloading occurs when multiple methods in the same class have the same name but different parameter lists (number or type of arguments).
  • Compile-Time Polymorphism: The method to be called is determined at compile-time based on the method signature.
  • Example Use Case in Selenium: Writing reusable functions with different parameters.


Overloading Example in Selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class SeleniumOverloadingExample {
    WebDriver driver;

    // Constructor
    public SeleniumOverloadingExample(WebDriver driver) {
        this.driver = driver;
    }

    // Click method with locator as By
    public void clickElement(By locator) {
        driver.findElement(locator).click();
    }

    // Overloaded Click method with WebElement
    public void clickElement(WebElement element) {
        element.click();
    }

    // Overloaded Click method with String (XPath as String)
    public void clickElement(String xpath) {
        driver.findElement(By.xpath(xpath)).click();
    }

    public static void main(String[] args) {
        WebDriver driver = // initialize WebDriver (e.g., new ChromeDriver());
        SeleniumOverloadingExample example = new SeleniumOverloadingExample(driver);

        // Example calls
        example.clickElement(By.id("submitBtn"));               // By locator
        WebElement element = driver.findElement(By.name("q"));  // WebElement
        example.clickElement(element);                          // WebElement
        example.clickElement("//button[@type='submit']");       // XPath as String
    }
}
        

2. Overriding in Java

  • Definition: Overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.
  • Runtime Polymorphism: The method to be called is determined at runtime based on the object's type.
  • Example Use Case in Selenium: Customizing browser actions or extending existing WebDriver functionalities.


Overriding Example in Selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

class CustomChromeDriver extends ChromeDriver {
    @Override
    public void get(String url) {
        System.out.println("Opening URL in Chrome: " + url);
        super.get(url);  // Call parent class's method
    }
}

class CustomFirefoxDriver extends FirefoxDriver {
    @Override
    public void get(String url) {
        System.out.println("Opening URL in Firefox: " + url);
        super.get(url);  // Call parent class's method
    }
}

public class SeleniumOverridingExample {
    public static void main(String[] args) {
        WebDriver driver;

        // Use Custom ChromeDriver
        driver = new CustomChromeDriver();
        driver.get("https://example.com");

        // Use Custom FirefoxDriver
        driver = new CustomFirefoxDriver();
        driver.get("https://example.com");
    }
}        


Article content


To view or add a comment, sign in

More articles by Subrat Jyetki

Explore content categories