Overriding is a concept in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. It enables polymorphism, where the same method name c
Blog
Hey everyone! Let's dive into some Java fundamentals today, especially for those who are venturing into automation testing.
Here's a quick breakdown of key concepts and their relevance in the world of automation.
๐บ 1๏ธโฃ Overriding in Java:
Overriding is a concept in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. It enables polymorphism, where the same method name can behave differently based on the object it's called on.
๐บ 2๏ธโฃ Main Points to Remember about Overriding:
๐๐ป Method signature:
The overriding method must have the same method signature (name, parameters, return type) as the overridden method in the superclass.
๐๐ป Access modifier:
The overriding method cannot have a more restrictive access modifier than the overridden method. It can have the same or wider access modifier.
๐๐ป Return type:
The return type of the overriding method must be the same as, or a subtype of, the return type of the overridden method.
๐บ 3๏ธโฃ Example Code of Overriding with Selenium:
// Superclass
class WebDriver {
public void get(String url) {
System.out.println("Opening URL: " + url);
}
}
// Subclass
class ChromeDriver extends WebDriver {
@Override
public void get(String url) {
System.out.println("Opening URL in Chrome: " + url);
}
}
// Usage
public class Main {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
}
}
In this example, the get method is overridden in the ChromeDriver subclass to provide a specific implementation for opening URLs in Chrome.
๐บ 4๏ธโฃ Overriding vs. Overloading:
Overriding occurs in inheritance, where a subclass provides a specific implementation of a method that is already defined in its superclass.
Overloading involves defining multiple methods in the same class with the same name but different parameters.
Understanding these concepts is crucial for building robust automation frameworks with Selenium, where customization and extensibility are key.
**
Introducing a curated package of Real-Time Interview Questions and Answers by an experienced Amazon SDET, covering topics like Selenium, API, Java, and more, gleaned from successful interview experiences at top companies: https://lnkd.in/gJrBGExe
Medium: https://medium.com/@sidharth.shukla19
Youtube: https://www.youtube.com/channel/UCwuAfFzAIAKU9qVYBVHiT3A
Copyright ยฉ2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV