method overriding in java code example
Example 1: what is method overloading and method overriding in Java?
Method overloading is providing two separate methods in a class with the same name but different arguments, while the method return type may or may not be different, which allows us to reuse the same method name.
Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type
Example 2: what is method overloading in java
Method overloading is providing
two separate methods in a class
with the same name but different arguments,
while the method return type
may or may not be different, which
allows us to reuse the same method name.
In my framework==
I use implicit wait in Selenium. Implicit wait
is an example of overloading. In Implicit wait
we use different time stamps such as SECONDS, MINUTES, HOURS etc.,
A class having multiple methods with
same name but different parameters
is called Method Overloading
Example 3: overloading and overriding in java
Method overloading is providing two separate methods in a class with the same name but different arguments, while the method return type may or may not be different, which allows us to reuse the same method name.
Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type
Example 4: what's method overriding
Overriding means same method name and same parameter,
occur in different class that has inheritance relationship.
we use method overriding to implement specific functionality to the method.
Example: get method
WebDriver driver = new ChromeDriver();
driver.get("URL") ==> opens the url from chrome
WebDriver driver = new FireFoxDriver();
driver.get("URL") ==> opens the url from Firefox
we can only override instance methods and method override
takes place in sub class.
instance method that we are going to override cannot be private and final
Example 5: overriding in java
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
}
}
Example 6: method overloading in java
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
private static void display(int a, int b){
System.out.println("Arguments: " + a + " and " + b);
}
public static void main(String[] args) {
display(1);
display(1, 4);
}
}