What is method overriding?
- If a subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
- In other words, if a subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
- Declaring a method in subclass which is already present in parent class is known as method overriding.
- In object-oriented terms, overriding means to override the functionality of an existing method.
- Method overriding is the example of run time polymorphism (Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic).
- When a method in a sub class has same name and type signature as a method in its super class, then the method is known as overridden method.
- The key benefit of overriding is the ability to define method that’s specific to a particular subclass type.
- Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
- Method overriding occurs in two classes that have IS-A (inheritance) relationship.
Understanding the problem without method overriding
Let’s understand the problem that we may face in the program if we don’t use method overriding.
1 2 3 4 5 6 7 8 9 10 |
class Vehicle{ void run(){System.out.println("Vehicle is running");} } class Bike extends Vehicle{ public static void main(String args[]){ Bike obj = new Bike(); obj.run(); } } |
1 2 |
Output:- Vehicle is running |
So to provide a specific implementation of run() method in subclass we use method overriding.
Method overriding examples
Example 1
Here we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so method overriding happens here.
1 2 3 4 5 6 7 8 9 10 |
class Vehicle{ void run(){System.out.println("Vehicle is running");} } class Bike2 extends Vehicle{ void run(){System.out.println("Bike is running safely");} public static void main(String args[]){ Bike2 obj = new Bike2(); obj.run(); } } |
1 2 |
Output:- Bike is running safely |
Example 2:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class animal{ void speak(){System.out.println("The animal speaks");} } class dog extends animal{ void speak(){System.out.println("Dog barks");} } class cat extends animal{ void speak(){System.out.println("Cat mew");} } class Test{ public static void main(String args[]){ dog d = new dog(); cat c = new cat(); d.speak(); c.speak(); } } |
1 2 3 |
Output:- Dog barks Cat mew |
Example 3:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class CarClass { public int speedLimit() { return 100; } } class Honda extends CarClass { public int speedLimit() { return 150; } public static void main(String args[]) { CarClass obj = new Honda(); int num= obj.speedLimit(); System.out.println("Speed Limit is: "+num); } } |
1 2 3 |
Output:- Speed Limit is: 150 Here speedLimit() method of class Honda is overriding the speedLimit()method of class CarClass. |
…continued in the next article Method Overriding in Java: – PART-2