Monday, February 10, 2020

Method Overriding In Java

Method Overriding In Java

Method Overriding(Runtime Polymorphism) is a kind of polymorphism like method overloading(Compile-time Polymorphism).

If a method in a subclass has the same prototype as a method of its superclass, then this method is said to be an overriding method in a subclass. Whenever an overridden method is called by the object of subclass than the version defined in the subclass will be always referred. Method Overriding provides the“single interface, multiple implementations” aspect of polymorphism. Method Overriding enables the subclass with the functionality to provide its own specific implementations.

 The same prototype means

  1.   same name
  2.   same signature( same type, order, and list of parameters)
  3.   same return type 

In simple words is a method in superclass is reimplemented in its subclass then it is known as method overriding.
Method Overriding in  java
Method overriding
Method overriding is a runtime polymorphism, i.e, the version of the method which will be invoked will be decided at run-time only. The object upon which the method is called will be responsible for the method invocation, and the method declared in the class of this object will be invoked.

Method Overriding rules

  • A superclass reference variable can refer to a subclass object.
  • Method calls in java are resolved dynamically at run time.
  • In Java, all variable knows their dynamic type.
  • Method calls are bound to the methods on the basis of the dynamic type of the receiver.
  • Methods must have same argument List and order
  • Methods must have the same return type
  • Subclass method should not have more restrictive access specifier
  • Subclass method should not throw new or broader checked exceptions
  • Final methods can’t be overridden
  • Constructors can’t be overridden

Example, Method Overriding


import java.util.Date;
 class A{
    public String message(String name)
    {
        return "Greetings,"+name;
    }
}
class B extends A{
    @Override
    public String message(String name) //Method message() is overridden
    {
       
        return "Greetings,"+name+"\n"+(new Date()).toString();
    }
}
public class OverridingDemo {

    public static void main(String[] args) {
            A a=new A();
            A ab=new B();
            B b=new B();
            System.out.println(a.message("pushpenda"));
            System.out.println(ab.message("pushpenda")); //Subclass version
            System.out.println(b.message("pushpenda"));    //Subclass version
    }
}
Output:
Greetings,pushpenda
Greetings,pushpenda
Mon Feb 10 14:05:17 IST 2020
Greetings,pushpenda
Mon Feb 10 14:05:17 IST 2020

Dynamic Method Dispatch

Method overriding forms the basis of “Dynamic Method Dispatch” which is one of the most powerful concepts of Java. It is the capability of Java to resolve the calls at runtime dynamically to overridden methods.


"When a method is called through the superclass reference variable, java determines which version of the method to call based upon the type of the object being referred at the time of call occurs"

Runtime polymorphism is useful for resolving

  1. Superclass reference variables
  2. Overridden methods

class A { //parent class
public void test()
{
    System.out.println("i m From A");
}
}
class B extends A{ //child class
@Override
public void test()  //Overriding method
{
    System.out.println("i m From B");
}
}
class C extends B{ //grand child
@Override
public void test()  //Overriding method
{
    System.out.println("i m From C");
}
}
public class OverridingDemo {
    public static void main(String[] args) {
        A a=new A();
        A ab=new B()
        A ac=new C();
        a.test();   //The version of method test()
        ab.test();  //will be chosen at the runtime
        ac.test();  //based on calling reference variable
    }
}
Output:
i m From A
i m From B
i m From C

Video Tutorial