+ 1

Overriding

If a base class and derived class each include a member function with the same name, which member function will be called by an object of a derived class and why.

26th Nov 2018, 6:51 AM
HighFlyer
HighFlyer - avatar
2 Answers
+ 3
Depends on the object... If object of parent class is used... The method in parent (base class) is invoked else the other Try this... I know It's in JAVA... But well Explained.. https://www.geeksforgeeks.org/overriding-in-java/
26th Nov 2018, 7:15 AM
$hardul B
$hardul B - avatar
+ 1
It depends on the type of object. Whether the object is of parent class type or derived class type. For eg: ParentA is a parent class and ChildB is its subclass and they both have a method called walk(). Now, ParentA obj = new ParentA(); obj.walk(); /*will call the walk() defined in parent class*/ ChildB obj2 = new ChildB(); obj2.walk(); /*will call the walk() defined in child class*/ So the object of a derived class call the method defined in derived class. Note: defined according to my knowledge in java.
27th Nov 2018, 4:40 PM
Mystery
Mystery - avatar