top of page

METHOD OVERRIDING IN JAVA

Writer's picture: Nidhi SharmaNidhi Sharma

Method Overriding is an approach which allows new implementation in a subclass with the same method signature (name, return type, and parameters). It means Overriding method has the same name, number of parameters, type of parameters and return type as the method that it overrides but it changes the implementation in the subclass.

Simplest real world example of method overriding is something like "A person inherits a car from his parent, so he doesn't want to change the car. He keeps the same car but changes the interior of it according to his convenience".


Now we will see another simple example in the form of Java code -


Here ElectricCar is the Superclass (or parent class or base class). Tesla and Rivian are its subclasses ( or child classes or derived classes).

This demonstrates method overriding, where the subclass methods speed() override the speed() method in the superclass ElectricCar.



 

package OOPS;


//Superclass

class ElectricCar {

void speed() {

System.out.println("in mph");

	}

}


// subclass

class Tesla extends ElectricCar {

void speed() {

System.out.println("TeslaSpeed : 200mph max");

}

}


// subclass

class Rivian extends ElectricCar {

void speed() {

System.out.println("RivianSpeed : 110mph max");

}

}


// Main class

class vehicle {

public static void main(String[] args) {

// Creating instances of ElectricCar and its subclasses

ElectricCar ec = new ElectricCar(); // instance of ElectricCar

ElectricCar tes = new Tesla(); // instance of Tesla

ElectricCar riv = new Rivian(); // instance of Rivian

/// Calling the method speed on each instance

ec.speed(); //Output - in mph

tes.speed(); //Output - TeslaSpeed : 200mph max

riv.speed(); //Output - RivianSpeed : 110mph max

}

}

 


OUTPUT :

 
in mph
TeslaSpeed : 200mph max
RivianSpeed : 110mph max
 


KEY POINTS ABOUT METHOD OVERRIDING IN JAVA :

 . Method overriding means to define a method that already exists in the superclass,i again n a subclass.

. Basic concept of I Overriding in Java is Inheritance and Polymorphism.

. Run-time polymorphism is implemented through method overriding, where different classes can have the same method with the same parameters. . That means the method that gets called is determined by the actual object type at runtime, not the reference type. While, Compile-time polymorphism is implemented through method overloading, which allows multiple methods with the same name but different parameters and return types.

. It is a good practice to use the @Override annotation when overriding a method: this annotation will check that the method is being overridden correctly, and will warn you if that's not the case (not mandatory though).

. We can overload private and static methods, but we can not override them.

. If any error occurs, can be caught at run-time.

. Parent classes and child classes form a hierarchy , So we can efficiently reuse code and override methods when necessary. This saves time and makes the code more organized and flexible.


RULES TO FOLLOW FOR METHOD OVERRIDING IN JAVA :

. The method name should be common and the same as it is in the parent class.

. The method signature (parameter list, return type) in the method must be the same as in the parent class.

. There must be an inheritance connection between classes.

. All the abstract methods in the parent class should be overridden in the child class.


LIMITATIONS OF METHOD OVERRIDING IN JAVA :

. Static methods can not be overridden, because If a subclass has a static method with the same signature (name and parameters) as a static method in the superclass then the method in the subclass hides the one in the superclass. (Or we can say, static method in the parent class will be hidden from the child class.)

. Main methods can not be overridden, as it's a Static method.

. Private methods can not be overridden, as it is not accessible outside the class.

. Final methods can not be overridden, as it is marked as a final and cannot be modified. The final keyword puts a stop to being an inheritance. Hence, if a method is made final it will be considered final implementation and no other class can override the behavior.

. Constructors can not be overridden like methods. Overriding a method means providing a new implementation in a subclass with the same method signature (name, return type, and parameters). Constructors, however, do not have a return type and are not inherited by subclasses.


EXCEPTION HANDLING WITH METHOD OVERRIDING IN JAVA :

Overriding deals with exception-handling with these ways :

1. When the parent class doesn’t declare an exception, the child class can declare only unchecked exceptions.

2. When the parent class has declared an exception, the child class can declare the same exception, not any other exceptions.

3. When the parent class has declared an exception, the child class can declare without exceptions.

4. Thrown checked exceptions, if any, can be removed or reduced by the overriding method. This means that the overriding method can throw the same checked exception as the overridden method, or a subclass of that checked exception, but not a broader exception. This restriction does not apply to unchecked exceptions.


Everything comes with some pros and cons, so here are some of those -


PROS OF METHOD OVERRIDING :

. Customized Functionality: Subclasses can provide their own implementations of inherited methods through overriding, allowing them to edit functionality to meet some requirements

. Flexibility in Method Invocation: Overriding allows developers to call the same method on different objects, with each object executing its specific implementation.

. Encapsulation: Method overriding can help using encapsulation by controlling the access and behavior of methods in subclasses. This ensures appropriate data manipulation.

. Framework Customization: In frameworks or libraries, method overriding allows developers to adapt and customize the behavior of predefined methods to meet their specific needs.


 CONS OF METHOD OVERRIDING :

. Method Combination Limit: Overriding methods can not combine the behavior of multiple parent class methods, as only a single overridden method can exist.

. Method Signature restriction: Overridden methods must maintain the same method signature as the original method, restricting changes to parameters or return type.

. Access Control Limit: Overridden methods must have the same or less restrictive access modifiers than the original method. This limits access control options.



That’s all about the Method Overriding in Java. Thank you all for reading till the end. Happy Learning!!

123 views

Recent Posts

See All
bottom of page