Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers, runtime polymorphism


In this core java tutorial we will learn what is Method overriding in java.

Contents of page :
  • What is Method overriding in java ?
  • Method overriding - Method of superclass is overridden in subclass when overriding method of subclass in java
    • (Method definition formation) Let’s make above 5 terms easy to remember by diagram/ understand method overriding by diagram  
  • 10 Features of Method overriding in java
  • Program 1 -  to understand Method overriding - Method of superclass is overridden in subclass in java
  • Program 2 - why we need Method overriding in java ?
  • @Override annotation in java
    • Problem without @Override annotation in java
    • Program 3 - Let’s say developer wanted to override food() of Animal in Lion class and by mistake he wrote fod() in place of food().
    • Solution with @Override annotation
  • What is Covariant return in java?
    • Program 4 - Covariant return in java



What is Method overriding in java?
Method of superclass is overridden in subclass to provide more specific implementation in java.

Real time Example of method overriding in java-
Different animals eat different food, like Lion eat flesh and Goat eat grass. So we can have generic SuperClass which tells that Animal might eat flesh, grass or may be some other thing.
Now, we can have SubClasses like Lion which more specifically that Lion eat flesh.
So at runtime, rather than calling food() method of SuperClass, food() method of subclass will be called and this way we could derive advantage of creating more specific SubClasses and overriding method. We will elaborate this in Program 1.
/*
* superclass - Animal
*/
class Animals {
   void food() {
          System.out.println("Animal may eat flesh, grass or ....");
   }
}
/*
* subclass of Animal - Lion
*/
class Lion extends Animals {
   @Override
   void food() {
          System.out.println("Lion eat - flesh");
   }
}


Method overriding - Method of superclass is overridden in subclass when overriding method of subclass in java -
  1. Method name - Overriding method same name as of superclass method in java,

  1. Access modifier - Overriding method must not have more restrictive access modifier in java.
    • Example 1 - public method cannot be overridden by private method in java.
    • Example 2 - default method can be overridden by default, protected or public method in java.

  1. Return type - Java allow method overriding by changing the return type, but only Covariant return type are allowed. (see Program 4)

  1. Number of parameters - Overriding method must have same number of parameters in java.

    • Overriding method must not throw new or broader checked exception in java,
    • though Overriding method may throw new narrower(subclass) of checked exception or
    • Overriding method can throw any runtime exception in java.



(Method definition formation) Let’s make above 5 terms easy to remember by diagram / understand method overriding by diagram -
Method definition is formed by using following 5 terms -

Note : Return type may be void, in that case method doesn’t return anything in java.

10 Features of Method overriding in java -
  1. Call to overridden method is bonded at runtime in java.

  1. Method overriding concept is also known as runtime time polymorphism in java.

  1. Only instance methods can be overridden in java.

  1. private methods can’t be overridden in java, because private methods are not inherited in subClass.

  1. final methods can’t be overridden in java, because final methods are not inherited in subClass.


  1. Main method also can’t be overridden in java, because main is static method and static methods can’t be overridden in java (as mentioned in above point)

  1. It’s important to know that instance variables are never overridden in java.

  1. Overriding method must not have more restrictive access modifier in java.




Program 1 -  to understand Method overriding - Method of superclass is overridden in subclass in java
Different animals eat different food, like Lion eat flesh and Goat eat grass. So we can have generic SuperClass which tells that Animal might eat flesh, grass or may be some other thing.

Now, we can have SubClasses like Lion and Goat which more specifically that Lion eat flesh and Goat eat grass.

So at runtime, rather than calling food() method of SuperClass, food() method of subclass will be called and this way we could derive advantage of creating more specific SubClasses and overriding method.
/*
* superclass - Animal
*/
class Animals {
   void food() {
          System.out.println("Animal may eat flesh, grass or ....");
   }
}
/*
* subclass of Animal - Lion
*/
class Lion extends Animals {
   @Override
   void food() {
          System.out.println("Lion eat - flesh");
   }
}
/*
* subclass of Animal - Goat
*/
class Goat extends Animals {
   @Override
   void food() {
          System.out.println("Goat eat - grass");
   }
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class MyClass {
   public static void main(String[] args) {
          Animals lion = new Lion();
          lion.food();
          Animals goat = new Goat();
          goat.food();
   }
}
/*OUTPUT
Lion eat - flesh
Goat eat - grass
*/

Output of the program in java shows that
Lion eat - flesh
Goat eat - grass

we are sure what Lion eat flesh and Goat eat grass specifically, which shows we derived advantage of creating more specific SubClasses.




Program 2 - why we need Method overriding in java ?
Different animals eat different food, like Lion eat flesh and Goat eat grass. So we can have generic SuperClass which tells that Animal might eat flesh, grass or may be some other thing.

Now, let’s say we build SubClasses like Lion and Goat which does not override food() method, than food() method of SuperClass Animal will be inherited in Lion and Goat, and we will fail to derive advantage of creating more specific SubClasses.
/*
* superclass - Animal
*/
class Animals {
   void food() {
          System.out.println("Animal may eat flesh, grass or ....");
   }
}
/*
* subclass of Animal - Lion
*/
class Lion extends Animals {
}
/*
* subclass of Animal - Goat
*/
class Goat extends Animals {
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class MyClass {
   public static void main(String[] args) {
          Animals lion = new Lion();
          lion.food();
          Animals goat = new Goat();
          goat.food();
   }
}
/*OUTPUT
Animal may eat flesh, grass or ....
Animal may eat flesh, grass or ....
*/

Output of above program shows that Animal may eat flesh, grass or .... , still we are not sure what Lion and Goat eat specifically, which shows we failed to derive advantage of creating more specific SubClasses.





@Override annotation in java
@Override annotation is used for compile time check to ensure that SubClass have successfully overridden method of SuperClass.
It helps in avoiding spell checks done by developers at runtime.

/*
* superclass
*/
class Animals {
   void food() {
          System.out.println("Animal may eat flesh, grass or ....");
   }
}
/*
* subclass
*/
class Lion extends Animals {
   @Override
   void food() {
          System.out.println("Lion eat - flesh");
   }
}



Problem without @Override annotation in java
Program 3 - Let’s say developer wanted to override food() of Animal in Lion class and by mistake he wrote fod() in place of food().
Than at runtime, food() method of Superclass will be inherited in Lion and get called when lion.food() is called and we will get undesirable output.
/*
* superclass - Animal
*/
class Animals {
   void food() {
          System.out.println("Animal may eat flesh, grass or ....");
   }
}
/*
* subclass of Animal - Lion
*/
class Lion extends Animals {
   void fod() {
          System.out.println("Lion eat - flesh");
   }
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class MyClass {
   public static void main(String[] args) {
          Animals lion = new Lion();
          lion.food();
   }
}
/*OUTPUT
Animal may eat flesh, grass or ....
*/



Solution with @Override annotation in java
Let’s say developer wanted to override food() of Animal in Lion class and by mistake he wrote fod() in place of food().
Than because of @Override annotation he will face compilation error.



What is Covariant return in java?
Java allow overriding by changing the return type, but only Covariant return type are allowed.
Example -
return type of SuperClass’s myMethod is  SuperClass but
SubClass’s myMethod returns  SubClass.

Covariant returns in java means overriding method can return SubClass.
Example -
Program 4 - Covariant return in java
class SuperClass{
   public SuperClass myMethod() {
          return new SuperClass();
   }
}
class SubClass extends SuperClass{
   @Override
   public SuperClass myMethod() {
          return new SubClass();
   }
}


Summary >
In this core java tutorial we learned What is Method overriding in java. Method overriding - Method of superclass is overridden in subclass when overriding method of subclass in java . Understood method overriding by diagram. 10 Features of Method overriding in java. Program to understand Method overriding. Why we need Method overriding in java. @Override annotation in java. What is Covariant return in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type



Labels: Core Java
eEdit
Must read for you :