Method Overriding
Method Overriding
• The method must have the same name as in the parent class.
• The method must have the same parameter as in the parent class.
Example program
public class Vehicle { OUTPUT:-
void run(){ Vehicle is running.
System.out.println("Vehicle is running."); Bike is running safely.
}
}
class Bike extends Vehicle{
void run(){
System.out.println("Bike is running safely.");
}
public static void main(String args[]){
Vehicle obj1=new Vehicle();
Vehicle obj2=new Bike();
obj1.run();
obj2.run();
}
}