Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

0
Can you provide me with example of method overloading and method overriding used in Selenium Framework using Java?

Open 1 Answers 2817 Views
0 1
Can you please provide me real-time examples of methods we have used as Java Polymorphism we have used in Selenium webdriver. I want the only practical example and not the definition of Polymorphism like Overloading and Overriding,

I know what is Overloading and Overriding but I need the real-time example which is used in Selenium webdriver frameworks.

Thanks in advance...
1Comments
commented by
moved by

No one in the group to provide answer.....Amazing blush......!

1 Answer

0
0

Overloading happens when 2 methods in the same class have the same name but different parameters.

Overriding means having 2 methods with the same name and same parameters, one being in a parent class and the other in a child class that inherits from the parent class.

Method overloading example

You want to have your own Listbox class to interact with dropdown lists.

The Listbox class is just a wrapper around the Select class.

You want it to have simpler method names for selecting list options:

public class Listbox {
 
  Select list;
 
  public Listbox(Select list) {
    this.list = list;
  }
 
  public void select(int i) {
    this.list.selectByIndex(i);
  }
 
  public void select(String text) {
    this.list.selectByVisibleText(text);
  }
 
  public void deSelect(int i) {
    this.list.deselectByIndex(i);
  }
 
  public void deSelect(String text) {
    this.list.deselectByVisibleText(text);
  }
 
  //other methods
 
}

The class has 2 select methods, one with an integer parameter, the other with a string parameter.

The 2 methods have the same name and both have no return type.

This is an example of overloading.

The difference between the 2 methods is being made by the parameter type.

The same applies to the deSelect() methods.


Method overriding example

Method overriding happens when you have child classes inheriting from parent classes.

For example, you may have a base page class with methods that work for any page:

public abstract class BasePage {
 
  String name;
  String url;
  String title;
  WebDriver driver;
 
  public BasePage(String name, String title, String url, WebDriver drv) {
    this.name = name;
    this.title = title;
    this.url = url;
    this.driver = driver;
  }
 
  protected String title() {
    return this.title;
  }
 
  protected String url() {
    return this.url;
  }
 
  protected String name() {
    return this.name;
  }
 
  protected WebDriver driver() {
    return this.driver;
  }
 
  protected boolean isDisplayed() {
    return this.title.equalsIgnoreCase(this.driver.getTitle()) &&
           this.url.equalsIgnoreCase(this.driver.getCurrentUrl());
  }
}

isDisplayed() method works by checking that the title and URL of the page are equal to the title and URL of the current page.

HomePage class inherits from BasePage class:

public class HomePage extends BasePage { 

  public HomePage(WebDriver driver) {

    super(“HomePage”, 

          “Home Page Title”,

          “http://www.abcdef.com/”, 

          driver);

  }

  //other methods

}

src: Quora Answer

answered by (552 points)

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
...