What is difference between overriding and overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is difference between overriding and overloading?

5th Nov 2016, 8:11 PM
rakeshdas - avatar
4 Answers
+ 4
given a class (C++ for instance) A and a class B which inherits from it: class A{ public: void func(){cout << "i am your father!" << endl;} } class B: public A{ public: void func(){cout << "NO, IT'S NOT TRUE!" << endl;} } function func() in class B is overriding the one which it inherita from class A the implications are when creating an object of class A, you naturally can only reach the method in that class but when creating an object of class B, you can only reach the overriden method (at least not until you declare the parent method as virtual) now for overloading (again in C++) take the class: class KillerRobot{ public: void attack(int atkPwr){cout << atkPwr << " Damage was made!" << endl;} void attack(){cout << rand() << " Damage was made!" << endl;} } as observed there are two attack methods one which deals an input int paramater as the damage while the other generate some random damage value hope this clear this up ;p
7th Nov 2016, 6:52 PM
Burey - avatar
+ 3
Overriding means having 2 methods with the same method name and parameters, that is the same method signature. One of the methods is in the parent class and the other in the child class. On the other hand, overloading occurs when two or more methods in one class have the same method name but different parameters.
7th Nov 2016, 1:37 AM
dariusPianotised - avatar
+ 2
here is an example in java but you may want to check to see difference or understand the meanings of overriding and overloading https://code.sololearn.com/c0awDMpDLukF/?ref=app
11th Sep 2017, 3:06 PM
Melih Melik Sonmez
Melih Melik Sonmez - avatar
plz anyone can explain it with example?
7th Nov 2016, 3:32 PM
rakeshdas - avatar