0% found this document useful (0 votes)
17 views4 pages

Method Overriding

Method overriding in object-oriented programming allows a subclass to provide a specific implementation of a method defined in its parent class. It is used for runtime polymorphism and requires the method to have the same name and parameters as in the parent class. An example demonstrates this concept with a Vehicle class and a Bike subclass, showing how both methods can be executed based on the object type.

Uploaded by

Faiz Dosani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Method Overriding

Method overriding in object-oriented programming allows a subclass to provide a specific implementation of a method defined in its parent class. It is used for runtime polymorphism and requires the method to have the same name and parameters as in the parent class. An example demonstrates this concept with a Vehicle class and a Bike subclass, showing how both methods can be executed based on the object type.

Uploaded by

Faiz Dosani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overriding

• Method overriding is a concept in object-oriented programming


where a subclass provides a specific implementation of a method that
is already defined in its parent class.
Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.

• Method overriding is used for runtime polymorphism.


Rules for Java 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();
}
}

You might also like