overriding value in java code example
Example 1: 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 2: overriding in java
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.
Examples are get and navigate methods
of different drivers in Selenium .
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: 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