Overriding in Java is a fundamental concept of object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its parent class. This is crucial for achieving runtime polymorphism and designing flexible, reusable, and maintainable code.
This guide explains overriding in Java in detail, from basic syntax and rules to practical use cases and advanced examples, suitable for beginners to intermediate learners.
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in the parent class. The overridden method must have the same name, return type, and parameter list as the parent class method.
class Parent { void display() { System.out.println("This is the parent class method."); } } class Child extends Parent { @Override void display() { System.out.println("This is the child class method."); } }
In Java, method overriding allows a subclass to provide a specific implementation of a method defined in its parent class. Starting from Java 5, covariant return types were introduced, allowing the overriding method to return a subtype of the parent method's return type.
Covariant return types mean that if a parent class method returns a type parent, the overriding method in a subclass can return child (a subclass of Parent). This provides more flexibility and type safety.
class Parent { Parent getObject() { System.out.println("Returning Parent object"); return new Parent(); } } class Child extends Parent { @Override Child getObject() { System.out.println("Returning Child object"); return new Child(); } } public class Main { public static void main(String[] args) { Parent parentRef1 = new Parent(); Parent parentRef2 = new Child(); parentRef1.getObject(); // Calls Parent's method parentRef2.getObject(); // Calls Child's overridden method } }
Since Java 5, methods can have covariant return types, meaning the overriding method can return a subtype of the parent method's return type.
class Parent { Parent getObject() { return new Parent(); } } class Child extends Parent { @Override Child getObject() { return new Child(); } }
| Feature | Overriding | Overloading |
|---|---|---|
| Definition | Subclass provides a new implementation of parent class method | Same class has multiple methods with same name but different parameters |
| Compile-time vs Runtime | Runtime (polymorphism) | Compile-time (method signature) |
| Return Type | Same or covariant type | Can be different |
| Inheritance Required | Yes | No |
Method overriding is widely used in Java frameworks and applications, including:
class Vehicle { void start() { System.out.println("Vehicle is starting"); } } class Car extends Vehicle { @Override void start() { System.out.println("Car is starting with key ignition"); } } class Bike extends Vehicle { @Override void start() { System.out.println("Bike is starting with kick start"); } } public class Main { public static void main(String[] args) { Vehicle v1 = new Car(); Vehicle v2 = new Bike(); v1.start(); // Car's start() is called v2.start(); // Bike's start() is called } }
This demonstrates runtime polymorphism where the method call resolves to the subclass method at runtime.
Method overriding in Java is a key feature of object-oriented programming that allows subclasses to provide specific implementations of methods defined in parent classes. By understanding overriding rules, syntax, and best practices, developers can write flexible, reusable, and polymorphic code that adapts at runtime.
Method overriding occurs when a subclass provides a specific implementation for a method already defined in its parent class with the same name, return type, and parameters.
Overriding is runtime polymorphism where a subclass changes the behavior of a parent class method. Overloading is compile-time polymorphism where methods have the same name but different parameter lists within the same class.
No, private methods are not visible to subclasses and therefore cannot be overridden. They can only be redefined in the subclass.
The @override annotation ensures that the method is correctly overriding a method from the parent class. It helps catch errors at compile-time if the method signature doesn't match.
No, static methods belong to the class rather than an instance and cannot be overridden. They can be hidden by defining a method with the same name in the subclass.
Copyrights © 2024 letsupdateskills All rights reserved