Method Overriding in Java

Table of Contents

It is easy to get confused while comparing between Overloading and Overriding, but it should be made clear that both of them are used under separate conditions.

Now, in this tutorial, we will be dealing with the concept of Method Overriding, and when it is used. First of all, let’s understand the rules that guide the implementation of Method Overriding.

Rules to be mentioned:

  • There must be an Inheritance relationship.
  • The method must have the same name as of the parent class
  • The method must have the same parameter as of the parent class.
  • The method should be of Instance type, which means you can not use a static method for applying Method Overriding.

What is Method Overriding?

If the subclass (child class) has the same method, same as declared in the parent class, it is known asĀ Method Overriding in Java.

Now, let’s look at the syntax of using it.

class Simple
{  
  void meth()
  {
    System.out.println("Updated in Class Simple");
  }  
}  
class Sample extends Simple
{   
  void meth()
  {
   System.out.println("Updated in Class Sample");
  }  
  public static void main(String args[])
  {  
    Sample obj = new Sample();
    obj.run();
  }  
}

So, after having a look at the above code snippet, we can easily conclude that Method Overriding actually performs Runtime Polymorphism and always keeps the last updated or latest updates value.

Previous Method Overloading in Java

Leave a Reply