what is overriding in java

1 year ago 28
Nature

Method overriding is a feature in Java that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, return type, and parameters as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

Method overriding is one of the ways by which Java achieves Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.

Overriding is useful because it allows Java to accept polymorphism at runtime, and it is also another way Java embraces polymorphisms "one application, many methods" aspect. Overridden methods allow one to call methods from any derived class object without identifying the form of the modified super-class.

In summary, method overriding is a way to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. It is useful for achieving Run Time Polymorphism and allows one to call methods from any derived class object without identifying the form of the modified super-class.