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.
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:
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.
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:
β 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
Copyright Β©2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV