10 Difference between Method overloading and Method overriding in java - in detail with programs


In this core java tutorial we will learn what are differences between Method overloading and Method overriding in java.


Method overloading and Method overriding forms base of core java. It’s very important to differentiate between two. Both have been used intensively in java Api’s. This is one the most prominently asked important  interview question in java.





Method overloading
Method overriding
1
When a class have same method name with different argument, than it is called method overloading.



Method overriding - Method of superclass is overridden in subclass to provide more specific implementation.




2
Method is overloaded by - keeping same name of method and only changing number of arguments

Let’s compare with method overriding in java.

  1. Method name - same method name.

  1. Access modifier - Does not matter.










  1. Return type - Does not matter.




  1. Number of parameters in java - Have different number of parameters
  2. Exception thrown - Does not matter.

In Method overriding - Method of superclass is overridden in subclass when overriding method of subclass in java -


  1. Method name - Have same name as of superclass method,
  2. Access modifier - Must not have more restrictive modifier. Example - public method cannot be overridden by private method.
  1. Return type - Java allow overriding by changing the return type, but only Covariant return type are allowed in java.
  2. Number of parameters in java - Have same number of parameters in java.
    • Overriding method must not throw new or broader checked exception,
    • though Overriding method may throw new narrower(subclass) of checked exception or
    • Overriding method can throw any runtime exception in java.


3
Method overloading is generally done in same class but can also be done in SubClass (See Program 3)
Method overriding is always done in subClass in java.
4
Both Static and instance method can be overloaded in java.
Only instance methods can be overridden in java.


5
Main method can also be overloaded in java (In Program 4)
Main method 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)
6
private methods can be overloaded in java.

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


7
final methods can be overloaded in java.

final methods can’t be overridden in java, because final methods are not inherited in subClass in java.
8
Call to overloaded method is bonded at compile time in java.
Call to overridden method is bonded at runtime in java.
9
Method overloading concept is also known as compile time polymorphism in java.
Method overriding concept is also known as runtime time polymorphism in java.
10
Example  - Please see below
Example - Please see below



Now we will learn differences between Method overloading and Method overriding in java with program and examples.


Example of method overloading in java -
   /*
   * Method to calculate sum of 2 arguments
   */
   public void sum(int x, int y) {
          System.out.println("sum of 2 arguments = "+ (x+y));
   }
   /*
   * Method to calculate sum of 3 arguments
   */
   public void sum(int x, int y, int z) {
          System.out.println("sum of 3 arguments = "+ (x+y+z));
   }
sum() method logically perform almost similar tasks and the only difference is in number of arguments. Method overloading enables same method name sum() to be reused in program in java.


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");
   }
}



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

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.

Summary >
So in this core java tutorial we learned what are 10 important differences between Method overloading and Method overriding in java.

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 in java


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


Difference between String, StringBuffer and StringBuilder in java - In depth coverage in java


Constructor in java - Constructor chaining, access modifiers with constructors, constructor overloading, exception thrown, constructors are not inherited


No comments:

Post a Comment