🔺 Telegram Group: https://t.me/+FTf_NPb--GQ2ODhl
📌 YouTube channel: https://lnkd.in/gGUGmb-P
Method Overloading in Java
Method overloading is a feature in object-oriented programming languages like Java that allows a class to have multiple methods with the same name but with different parameters. These methods can perform similar or related tasks but may accept different types or numbers of parameters.
In method overloading:
- Methods must have the same name.
- Methods must be defined within the same class.
- Methods must have different parameter lists (different types or different numbers of parameters).
- The return type of the methods may or may not be the same.
When a method is invoked, the compiler determines which version of the overloaded method to call based on the number and types of arguments provided. This decision is made at compile time, and the appropriate method is then called at runtime.
Method overloading provides several benefits:
- It improves code readability and maintainability by allowing developers to use intuitive method names for different variations of a task.
- It enhances code reusability by allowing methods to perform similar tasks with different inputs.
- It provides flexibility in method design, enabling the creation of more intuitive and expressive APIs.
Overall, method overloading is a powerful tool that allows developers to write cleaner, more expressive code while providing flexibility and versatility in method design.
Scenario: Handling Different Types of Click Actions
In Selenium test automation, you often encounter scenarios where you need to perform various types of click actions on web elements. These actions can include simple clicks, double clicks, and right clicks. By using method overloading, you can create flexible and intuitive methods to handle these different types of click actions efficiently.
javaimport org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.interactions.Actions;public class ClickActionsHelper {private WebDriver driver;private Actions actions;public ClickActionsHelper(WebDriver driver) {this.driver = driver;this.actions = new Actions(driver);}// Method for performing a simple clickpublic void click(WebElement element) {actions.click(element).build().perform();}// Method for performing a double clickpublic void click(WebElement element, boolean doubleClick) {if (doubleClick) {actions.doubleClick(element).build().perform();} else {click(element); // Invokes the simple click method}}// Method for performing a right clickpublic void click(WebElement element, boolean doubleClick, boolean rightClick) {if (rightClick) {actions.contextClick(element).build().perform();} else {click(element, doubleClick); // Invokes the appropriate click method based on parameters}}}
In this example, we have a ClickActionsHelper
class that encapsulates methods for handling different click actions using Selenium's Actions
class. We use method overloading to define multiple click
methods with varying parameters:
- The first
click
method takes aWebElement
parameter and performs a simple click action. - The second
click
method overloads the first one and adds a boolean parameter to indicate whether it should perform a double click. Depending on the value of this parameter, it either performs a double click action or invokes the simple click method. - The third
click
method further overloads the second one and adds another boolean parameter to indicate whether it should perform a right click. Similarly, it either performs a right click action or invokes the appropriate click method based on the parameters provided.
By using method overloading in this manner, we can handle different types of click actions with ease and readability, enhancing the maintainability and flexibility of our test automation code.
GENERIC EXAMPLES
public static void main(String as[]) throws Throwable {
System.out.println("sum is: " + sum(14,0) + " sum of three params is: " + sum(1,2,3));
}
public static int sum(int a, int b) throws Exception {
int sum = a+b;
return sum;
}
private static int sum(int a, int b, int c) {
int sum = a+b+c;
return sum;
}
}
Output:
sum is: 14
sum of three params is: 6
//In above example we have two method with same name as "sum" but we have pass different parameters which is an perfect example for method overloading or polymorphism.
*****
📌1:1 Call on Career Guidance: https://lnkd.in/ddayTwnq
****
📣 Looking to understand the advanced concepts in Java with practical examples from Test Automation ?
Learn SDET or Automation Testing along with API Testing, Rest Assured, Selenium, Appium, Jenkins, GIT, Docker, Generative AI with live doubt sessions, explore the details here: https://lnkd.in/giCxnJJ7.
No comments:
Post a Comment