Method Overriding


In the class hierarchy, when a subclass method is typed with the same name and signature as a method in its superclass, the subclass method asks to override the method in the superclass. When an override method is called from within its subclass it will always refer to the version of that method defined by the subclass. Then the method defined by the superclass will be hidden.

Conditions for overriding the method

Example of overriding methods

See the following program overrides a method of two classes:

Program
// Method overriding
 
// Create a superclass
class Phone
{
 void Version(double v)
 {
  System.out.println("This is the primary version of the phone: "+v);
 }
}
// Create a subclass that extends Phone
class UpgradeVersion extends Phone
{
 // Phone class Version () method is being overridden
 void Version(double Uv)
 {
  System.out.println("This is the upgrade version of the phone: "+Uv);
 }
}
public class Main 
{
  public static void main(String[] args) 
  {
    /* You already know that a reference variable in a superclass can point
     to its subclass or child class object. If you do not know, see the
     previous tutorials. */
     
    // Create reference variable of superclass
    Phone p1;
    UpgradeVersion uv=new UpgradeVersion();
    p1=uv;
    p1.Version(2.0);
  }
}

In the example above, a code sample is given to change the version of a mobile phone. So a superclass called "Phone" and a subclass or child class called "UpgradeVersion" has been created. Now a method called "Version (double v)" has been created inside the superclass which shows the output of the primary version of the phone. Suppose you need to upgrade the version of the phone, but no superclass method can be deleted or changed. That is why the method named "Version (double v)" with the same method name, same parameter, same return type has been created in the subclass. In which the version of that phone has been upgraded. This time during method calling we have called with superclass reference variable.


Dynamic Method Dispatch

The examples in the previous section show the mechanics of the override of the method, they do not show their strength. In fact, if there is nothing more to override the method than the namespace convention, it will be an interesting curiosity above all else, but its real value will be very low. However, this is not the case. The method forms the basis of one of the most powerful concepts of override Java: sending dynamic methods. The dynamic transmission method is the process by which the call is solved in run time instead of time set in the override method. The transmission of dynamic methods is important because Java thus applies run-time polymorphism.

Let's start with an important principle again: a superclass reference variable can refer to a subclass object. Java solves this time override calls at run-time. Here is how. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made during the run. When different types of objects are specified, different versions of the override method will be called. In other words, it refers to the type of object (not the type of reference variable) that determines which version will be executed in the override method. Therefore, if a superclass has a method that is overridden by the subclass when different types of objects are specified through the superclass reference variable, different versions of the method are implemented.

Program
// Dynamic Method Dispatch
class X
{
 void Show()
 {
  System.out.println("Inside X's Show method");
 }
}
class Y extends X
{
 void Show()
 {
  System.out.println("Inside Y's Show method");
 }
}
class Z extends X
{
 void Show()
 {
  System.out.println("Inside Z's Show method");
 }
}
public class Main 
{
  public static void main(String[] args) 
  {
    X x=new X();
    Y y=new Y();
    Z z=new Z();
    
    X r; // obtaina a reference of type X
    
    r=x; // r refers to an X object
    r.Show(); // calls X's version of Show
    
    r=y; // r refers to an Y object
    r.Show(); // calls Y's version of Show
    
    r=z; // r refers to an Z object
    r.Show(); // calls Z's version of Show
  }
}

This program creates one superclass called "X" and two subclasses of it, called "Y" and "Z". Subclasses "Y" and "Z" override "Show()" declared in "X". Inside the "main()" method, objects of type "X","Y", and "Z" are declared. Also, a reference of type "X", called "r", is declared. The program then in turn assigns a reference to each type of object to "r" and uses that reference to invoke "Show()". As the output shows, the version of "Show()" executed is determined by the type of object being referred to at the time of the call. Had it been determined by the type of the reference variable, "r", you would see three calls to the "X" "Show()" method.


Note : Final methods cannot be overridden.