What is a Method overriding in Java?

Method overriding is a feature in Java that allows a subclass to provide its own implementation of a method that is already defined in its superclass. When a subclass defines a method with the same name, return type, and parameter list as a method in its superclass, it is said to be overriding that method.

Table of Content :

When a method is called on an instance of the subclass, Java first looks for an implementation of the method in the subclass. If the subclass has defined its own implementation of the method, that implementation is called. Otherwise, Java looks for an implementation of the method in the superclass.

Method overriding is useful for creating more specialized behavior in subclasses. It allows you to reuse the code of a superclass while also customizing its behavior in the subclass. It also allows you to implement polymorphism, where objects of different types can be treated in a uniform way based on their common superclass.

To override a method in Java, you need to define a method in the subclass with the same name, return type, and parameter list as the method you want to override in the superclass. You also need to use the @Override annotation to indicate that you are intentionally overriding the method.

Example for method overriding in java?

class Animal { public void makeSound() { System.out.println("The animal makes a sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("The dog barks"); } } public class Main { public static void main(String[] args) { Animal animal = new Animal(); animal.makeSound(); // prints "The animal makes a sound" Dog dog = new Dog(); dog.makeSound(); // prints "The dog barks" Animal animal2 = new Dog(); animal2.makeSound(); // prints "The dog barks" } }

In this example, the Animal class has a makeSound() method that prints a generic message. The Dog class extends the Animal class and overrides the makeSound() method to print a specific message for dogs.

In the Main class, we create an instance of the Animal class and call its makeSound() method. We then create an instance of the Dog class and call its makeSound() method, which prints the overridden message for dogs.

Finally, we create another instance of Animal and assign it to a variable of type Animal. We then call the makeSound() method on this variable, but because the actual instance is a Dog object, the overridden makeSound() method in the Dog class is called and prints the message for dogs. This demonstrates the polymorphic behavior achieved through method overriding.

Method Overriding in Java - Benefits

Method overriding in Java provides several benefits, including:

Polymorphism: Method overriding allows objects of different classes to be treated uniformly based on their common superclass. This means that you can write code that works with objects of a superclass and their subclasses without knowing the specific subclass being used.

Code reuse: Method overriding allows subclasses to reuse code from their superclass. By overriding a method in the superclass, a subclass can inherit the method implementation from the superclass and modify it as needed, rather than having to write the entire method implementation from scratch.

Customization: Method overriding allows subclasses to customize the behavior of a method inherited from the superclass. By providing a specialized implementation of a method, a subclass can provide specific behavior that is unique to that subclass.

Flexibility: Method overriding allows you to change the behavior of a method in a subclass without affecting the behavior of the method in the superclass. This means that you can create specialized behavior in subclasses while still maintaining the general behavior of the method in the superclass.

Overall, method overriding is a powerful feature of Java that allows for more flexible, maintainable, and reusable code. It enables polymorphism, code reuse, customization, and flexibility, which are all important benefits in object-oriented programming.

what are the limitations of method overriding in java?

Method overriding in Java has some limitations that you should be aware of:

Final methods: You cannot override a method in the superclass if it is declared as final. This is because final methods cannot be changed or modified in any way, including through inheritance and overriding.

Private methods: You cannot override a private method in the superclass because it is not visible to the subclass. However, if a subclass declares a method with the same name and signature as a private method in the superclass, it is not considered as overriding, but rather as a new method in the subclass.

Static methods: You cannot override a static method in the superclass because static methods are not associated with any instance of the class. Instead, they are associated with the class itself, and therefore cannot be overridden.

Method signature: When overriding a method in the subclass, you must use the same method signature as the method in the superclass. This means that the method name, return type, and parameter types must all match. If any of these are different, the method is considered a new method in the subclass and not an override of the method in the superclass.

Exception throwing: When overriding a method in the subclass, you must ensure that the method does not throw any checked exceptions that are not declared in the method signature of the method in the superclass. If the subclass method throws a checked exception that is not declared in the superclass method signature, a compilation error will occur.

These limitations are important to keep in mind when using method overriding in Java. By understanding these limitations, you can avoid common mistakes and write more effective and efficient code.

In this article, we have seen the What is a Method Overriding in Java with examples along with benefits and limitations All source code in the tutorial can be found in the GitHub repository..