+ 1
What is difference between overriding and overloading?
4 ответов
+ 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
+ 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.
+ 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
0
plz anyone can explain it with example?