what's method overriding
// Base Class
class Parent {
void show(){
System.out.println("Parent's show()");
}
}
// Inherited Class
class Child extends Parent {
// This method overrides show() of Parent Class
@Override
void show(){
System.out.println("Child's show()");
}
}
// Main class
class Main {
public static void main(String[] args){
Parent obj = new Child();
// Calling Child Class Method
obj.show();
}
}
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
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
do{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += itemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += itemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while(ordering);
System.out.println("Total amount: " + runningTotal);
}