Wednesday, March 8, 2017

Method Overloading Vs Method Overriding

Method overloading

If two or more method in a class has same name but different parameters, it is known as method overloading.
Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing the number of arguments or by changing the data type of arguments. If two or more method has same name and same parameter list but differs in return type are not said to be overloaded method.

Different ways of Method overloading:

There are two different ways of method overloading

1. Method overloading by changing data type of Arguments

Example:
class Calculate
{
      void sum (int a, int b)
    {
          System.out.println(“sum is”+(a+b)) ;
    }
     void sum (float a, float b)
   {
         System.out.println(“sum is”+(a+b));
    }
Public static void main (String[] args)
     {
         Calculate cal = new Calculate();
         cal.sum (8,5); //sum(int a, int b) is method is called. 
         cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
     }
}
Output :
Sum is 13
Sum is 8.4
You can see that sum() method is overloaded two times. The first takes two integer arguments, the second takes two float arguments.

2. Method overloading by changing no. of argument.

Example :
class Area
{
       void find(int l, int b)
   {
        System.out.println(“Area is”+(l*b)) ;
   }
     void find(int l, int b,int h)
  {
       System.out.println(“Area is”+(l*b*h));
  }
public static void main (String[] args)
   {
       Area ar = new Area();
       ar.find(8,5);       //find(int l, int b) is method is called. 
       ar.find(4,6,2);   //find(int l, int b,int h) is called.
    }
}
Output :
Area is 40
Area is 48
In this example the find() method is overloaded twice. The first takes two arguments to calculate area, and the second takes three arguments to calculate area.

Method Overriding

When a method in a sub class has the same name and type signature as a method in its super class, then the method is known as overridden method. Method overriding is also referred to as runtime polymorphism. The key benefit of overriding is the ability to define the method that’s specific to a particular subclass type.

Example of Method Overriding

class Animal
{
    public void eat()
  {
        System.out.println(“Generic Animal eating”);
   }
}
class Dog extends Animal
{
      public void eat()   //eat() method overriden by Dog class.
   {
         System.out.println(“Dog eat meat”);
    }
}
As you can see here Dog class gives it own implementation of eat() method. Method must have same name and same type signature.
NOTE*: Static methods cannot be overridden because a static method is bounded with the class whereas instance method is bounded with an object.

Difference between Overloading and Overriding-

Method overloading

Method Overriding

Parameter must be different and name must be same.Both name and parameter must be same.
Compile time polymorphism.Run time polymorphism.
Increase readability of code.Increase reusability of code.
Access specifier can be changed.Access specifier most not be more restrictive than original method(can be less restrictive).
Method overloading is performed within the class.Method overriding occurs in two classes that have IS-A (inheritance) relationship.

No comments:

Post a Comment