Home >>Java Tutorial >Method Overriding in Java

Method Overriding in Java

Method Overriding in Java

Method Overriding is a condition in the Java language in which a subclass that is also known as the (child class) has the same method that has been declared in the parent class. In simple words, it can be understood as a case where a subclass provides the specific implementation of the method that has already been declared by one of its parent class. Please note that overriding generally means to override the functionality of an existing method in the terms of object-oriented.

The main advantage or we can say the benefit of method overriding is its ability to define a behavior that's specific to the subclass type that simply means that a subclass can implement a parent class method that will be based on its requirement. Overriding generally means to override the functionality of an existing method in Java in terms of object-oriented.

Here is an example of the method overriding in the Java language that will help you to understand the application aspect of it:

class Animal
{
  
public void move ()
  {
    
System.out.println ("Animals can move");

} 
} 

class Cat extends Animal
{
  
public void move ()
  {
    
System.out.println ("Cats can walk and run");

} 
} 
 
public class TestCat

{
  
 
public static void main (String args[])
  {
    
Animal a = new Animal ();	// Animal reference and object
    Animal b = new Cat ();	// Animal reference but Cat object
    
a.move ();			// runs the method in Animal class
    b.move ();			// runs the method in Cat class
} 
} 
Output:
Animals can move
Cats can walk and run

Here is the explanation of the above example; even though b is a type of Animal and it runs the move method in the Cat class. The main reason behind it is that in the compile time, the reference type is generally the one on which the check is performed. However, JVM generally figures out the object type and it would run the method that generally belongs to that particular object at the runtime. Hence, the program will basically compile in order as the Animal class has the method named move. Therefore, at the runtime, this will run the method that is specific for that object only.

Usage of Java Method Overriding

  • Method overriding is generally used in order to deliver the specific implementation of a method that has been already provided by its superclass.
  • Method overriding is known for its usage for the runtime polymorphism.

Rules for Java Method Overriding

There are certain rules that are generally implemented when the method overriding is used in the Java languages that are depicted below:

  • The method overriding in Java should have the same name as in the parent class.
  • The method overriding in Java should have the same parameter as in the parent class.
  • An IS-A relationship that is also known as inheritance should be there.
  • The argument list in the Java language must be exactly the same as that of the overridden method.
  • The return type must be the same or a subtype of the return type that has been declared in the original overridden method in the superclass of the method.
  • The access level of the Java method overriding cannot be more restrictive than that of the overridden method's access level.
  • Instance methods can generally be overridden in only if it is provided that they are inherited by the subclass.
  • A method that has been declared final cannot be overridden.
  • A method that has been declared static cannot be overridden instead it can be re-declared.
  • In the case where a method cannot be inherited then in that case it cannot be overridden also.
  • Any superclass method that is not declared private or final can be overridden by a subclass within the same package as the instance's superclass.
  • A subclass of the method overriding that is in a different package can only override the non-final methods that have been declared public or protected.
  • Overriding methods in the Java language can throw any uncheck exceptions that are, regardless of the fact that whether the overridden method throws the exceptions or not. As a fact, the overriding method must not throw any checked exceptions that are new or broader than the ones that have been declared by the overridden method in the Java language. Please note that the overriding method can generally throw narrower or fewer exceptions than the overridden method.
  • Constructors in the Java cannot be overridden by any method.