Method Overriding in Java

Method Overriding
If we define the same name for a subclass(child class) and its superclass(parent class) and when the object is created to the subclass, then the method of subclass overrides the superclass method. this is called method overriding
In brief, if we declare a method to a subclass, which is already present in the parent class, Overriding can be done now, where the subclass can give its own implementation to the method which is already present inside the parent class
info
Method overriding is used for runtime polymorphism
Example
Output
In the above example ,we created a object for the child class ,when show method is called then parent class show( ) is ovridden with the child class show( ),so the output will be Child MEthod
Dynamic Method Dispatch (Runtime Polymorphism)
The overriding is possible in run time and this is called dynamic method dispatch.
A parent class reference variable will be pointed to the child class object. Then the overridden method will be determined at the run time. this is also known as Upcasting
Syntax
we created a class named X,and we have created a child class named Y from parent class X
we are now creating a ref reference varibale for parent class X
created an object for the Y class using reference variable, this is known as up casting
Example
Output