Difference between overriding and overloading?

Difference between overriding and overloading

Difference between overriding and overloading?

Difference between overriding and overloading?

Overloading

  • Two methods are said to be overloaded if and only if both methods having the same name but difference arguments types.
  • In C-language method overloading concept is not available hence we can’t declare multiple methods with same name but different arguments types.
  • If there is a chance in arguments types compulsory we should go for new method name which increases the complexity of programming.
  • But in Java, we can declare multiple methods with same name but different arguments types. Such type of methods is called Overloaded methods.
  • Having overloading concept in java reduces the complexity of programming. Example…
package com.java4us;
class Car
{
 public int speedLimit() 
 {
 return 100;
 }
}
class Ford extends Car
{
 public int speedLimit()
 {
 return 150;
 }
 public static void main(String args[])
 {
  Car obj = new Ford();
  int num= obj.speedLimit();
  System.out.println("Speed Limit is: "+num); //150
 }
}
  • In overloading method resolution always take care by compile based on reference type hence overloading is also consider as compile-time polymorphism or static polymorphism or early binding. 
  • Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*