Whenever the same method name is existing in both base class and derived class with the same types of parameters or same order of parameters is known as Method Overriding. Here we will discuss Overriding in Java.
 

Advantage of Java Method Overriding
  • Method Overriding is used to provide specific implementation of a method that is already provided by its superclass.
  • Method Overriding is used for Runtime Polymorphism

Rules for Method Overriding
  • The method must have the same name as in the parent class.
  • The method must have the same parameter as in the parent class.
  • Must be IS-A relationship (inheritance).
  • The data types of the arguments along with their sequence must have to be preserved as it is in the overriding method.
  • Any method which is overriding can throw any unchecked exceptions, in spite of whether the overridden method usually method of parent class might throw an exception or not.
  • The private, static, and final methods can't be overridden as they are local to the class.                                                                                                                                                                                    

    Example of method overriding in Java

    In this example, we have defined the walk method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same and there is an IS-A relationship between the classes, so there is method overriding.

Program:

  class Walking
{  
void walk()
{
System.out.println("Man walking fastly");
}  
}  
class Man extends walking
{  
void walk()
{
System.out.println("Man walking slowly");
}
}

class OverridingDemo
{  
public static void main(String args[])
{  
Walking obj = new Walking();  
obj.walk();  
}
}

Output:

Man walking slowly

Do you Know?
Progamming is what actually means ?

What is software development ?

How we categorized software development in different manner.

C Programming
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle
We cover all the programming concepts in various programming languages, this tutorials are very help full for bigener as well as Experience developer, for the ease of understanding we categorized programming in different manner likewise.