Method Overriding in Java
Method overriding in Java is the process of providing a different implementation of a method in a subclass that is already defined in its superclass. When a subclass overrides a method, it provides its own implementation of the method, which will be invoked instead of the superclass implementation when the method is called on an object of the subclass.
Uses of Method Overriding
Polymorphism: Method overriding is a fundamental feature of polymorphism in object-oriented programming. It allows objects of different classes to be treated as objects of their common superclass. By overriding methods, the behavior of the superclass can be modified or extended in the subclass, enabling the flexibility to use objects interchangeably based on their actual type.
Specific Behavior: Method overriding allows subclasses to define specific behavior for a particular method. It allows subclasses to customize or specialize the inherited behavior to suit their specific needs. This promotes code extensibility and provides a way to adapt the inherited functionality to different contexts.
Enhancing or Refining Functionality: Method overriding enables subclasses to enhance or refine the functionality provided by the superclass. Subclasses can add additional operations, perform extra tasks, or modify the behavior of the inherited method while still retaining the original method signature.
Code Reusability: Method overriding promotes code reusability by allowing subclasses to reuse and build upon the existing methods provided by the superclass. Instead of rewriting the entire method logic, subclasses can reuse the common functionality and only modify or extend the parts specific to their requirements.
Rules for Method Overriding:
Inheritance: Method overriding is applicable only in the context of inheritance. The overriding method must be declared in a subclass that extends or inherits from the superclass containing the method to be overridden.
Method Signature: The overriding method must have the same method name, return type, and parameter list (including order and types) as the method being overridden in the superclass. The method signature is crucial for establishing the overriding relationship.
Access Modifiers: The overriding method cannot have a lower access modifier than the overridden method in the superclass. It can have the same or higher access modifier. The access modifiers include public, protected, default (package-private), and private.
Exception Handling: The overriding method can throw the same or narrower checked exceptions compared to the exceptions thrown by the overridden method. However, it cannot throw a broader checked exception or any new checked exception that is not part of the superclass method's exception list.
Example of Method Overriding
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
@Override
public void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw(); // Output: Drawing a circle
shape2.draw(); // Output: Drawing a square
}
}
In this example, the Shape class has a draw() method that is overridden in the Circle and Square subclasses. When the draw() method is called on objects of type Shape, the appropriate version of the method is invoked based on the actual object type. The overridden methods in the subclasses provide their own implementation, resulting in different output based on the specific object.
Method overriding is a powerful feature that allows subclasses to provide their own implementation of inherited methods. It facilitates polymorphism, code extensibility, and customization while promoting code reuse and modularity.