Method Overloading with Example from Automation

I have noticed that in interviews, folks are giving very random examples of overloading. So, I thought of sharing an example that makes more sense during an automation interview.

Mentor

Blog

Method Overloading with Example from AutomationI have noticed that in interviews, folks are giving very random examples of overloading. So, I thought of sharing an example that makes more sense during an automation interview.

βœ… What is OverLoading?

In Java, method overloading allows a class to have multiple methods having the same name but with different parameters. The compiler differentiates these methods based on the number or types of parameters. This enhances code readability and provides flexibility.

Key points about method overloading in Java:

  1. Method Signature:

    Method overloading is determined by the method signature, which includes the method name and the parameter types.Return type and access modifiers are not considered while overloading.

    2. Different Parameter Lists:

    Overloaded methods must have a different number or types of parameters. This is known as a difference in method signatures.

    3. Same Name, Different Functionality:

    Although methods share the same name, they can perform different tasks based on the parameters they receive.

    1. Example:

      public class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } public String add(String a, String b) { return a + b; }}

      In this example, the add method is overloaded with different parameter types (int, double, and String).

      Compile-Time Polymorphism:

      • Method overloading is an example of compile-time polymorphism (or static polymorphism) because the decision on which method to call is made at compile time.

        βœ… SCENARIO FROM AUTOMATION:

        For example, in Selenium test automation, you might have a scenario where you want to click on a web element based on different criteria. You can overload the click method to handle various situations. Here's a simplified example:

        βœ… CODE EXAMPLE

        public class WebElementClickHandler {

        private WebDriver driver;

        public WebElementClickHandler(WebDriver driver) {

        this.driver = driver;

        }

        // Click by WebElement

        public void click(WebElement element) {

        element.click();

        }

        // Click by locator using By

        public void click(By locator) {

        WebElement element = driver.findElement(locator);

        element.click();

        }

        // Click by locator and index

        public void click(By locator, int index) {

        WebElement element = driver.findElements(locator).get(index);

        element.click();

        }

        }

        In this example, the click method is overloaded with different parameters - WebElement, By (locator), and By with an additional index parameter. This allows users to click on elements in different ways, providing flexibility in Selenium test scripts.

        βœ… POINTS TO REMEMBER

        πŸ“Œ Same Method Name, Different Signatures:

        Overloaded methods in Java share the same name but have different parameter lists. The method signature includes the method name and the types and order of its parameters. Return types and access modifiers are not considered part of the signature.

        πŸ“Œ Compile-Time Resolution:

        Method overloading is resolved at compile time based on the number and types of arguments passed to the method. During compilation, the Java compiler determines which overloaded method to call by matching the provided arguments with the method signatures.

        *****πŸ“Œ1:1 Call for Guidance on how to start career in sdet & automation, tips to crack coding rounds: https://lnkd.in/ddayTwnq (Use code "Thanksgiving" for additional discount)

        ******

        πŸ”₯ For a more hands-on learning experience in automation with realistic examples, consider exploring a one-on-one guided session on end-to-end automation and SDET(API+UI+Mobile+DevOps+GenerativeAI) : https://lnkd.in/giCxnJJ7

        ******

        Learn how to use Generative AI to automation both UI and API tests using selenium and rest assured with prompts: https://lnkd.in/gBjxYAPN****

        #sidpost#java#linkedinconnection#testing#testautomation#career#technology#sdet#automation#qualityassurance#softwaretesting#selenium#seleniumautomation