- If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
Usage:-
- Method overriding is used to provide specific implementation of a method that is already provided by its super class.
- Method overriding is used for runtime polymorphism
Rules for Java Method Overriding:-
- method must have same name as in the parent class
- method must have same parameter as in the parent class.
- must be IS-A relationship (inheritance).
Example:–
{
void run()
{
System.out.println(“Vehicle is running”);
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println(“Bike is running safely”); //Method Overriding
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
0 Comments:
Post a Comment