0

Difference Between Method overriding and Method overloading.

If you heard these two terms and you feel confused you have came to right place.  Method Overloading and Method Overriding plays major role in writing a code. Here I will explain how they are different and when they are used. So lets start the difference between Method Overloading and Method Overriding.

Method overloading: Method overloading is a concept which enables us to make Methods with same name. It is also known as compile time polymorphism. The condition for Method Overloading is that the name of the Method should be same but the signature must be different. For ex

Void A(int a, int b);

Void A(int a, float b);

Void A(int a, int b, int c);

So like this here Method name is same but signature i.e. no. of arguments, type of arguments etc.  are different.
It can be very useful when we have to do same task in different conditions. For example if we want area of shapes we have this code:-


int area(int a)

{ int ar;

ar = a*a;

return ar;

}
Above code takes only one argument


int area (int a, int b)

{int ar;

ar = a*b;

return ar;

}

Above code can take two arguments. So here Method Overloading happened.


Method Overriding: Method Overriding is a concept in which methods are made with same name and same signatures too. It is generally used when we want to perform different task in a inherited class by the same function. Method Overriding is also known as runtime polymorphism. The condition for method Overriding are:-


1. The name and signatures of method must be      

   same.
2. The Overriding method should be public if the             

   Method in superclass is public.
3. Final or static method can’t be overridden.
4.Method that cannot be inherited can’t be   

  overridden.

Let’s look at example code

class Animal {
  public void makeSound() {
      System.out.println(“Grr…”);
  }
}
class Cat extends Animal {
  public void makeSound() {
      System.out.println(“Meow”);
  }
}
Here different class demand different use of same method makeSound.

Note the code shown is in the syntax of java but the concept of the topic is same in every OOP language.

for more programming stuff click|

https://www.devlopersguide.com/category/programming/

Hope you guys liked the post. For any queries or feedback comment down below.


 

ks007

Leave a Reply

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