+ 6

Method preference in Inheritance

Consider class A and class B. Let B be a subclass of A. If both the class contain method hello (), then if we call it using an object of B, say b, as b.hello () in the main, which of the methods will be called? Is it the same for C++ and Java?

10th Apr 2017, 2:41 PM
Pixie
Pixie - avatar
1 Answer
+ 4
The method from class B will be called. Class B overrides the 'hello()'-method from class A, beacause they have the same name and parameters (none, in this case). Because object 'b' is an instance of class B, the 'hello()'-method from class B will be called. This is the same for C++.
10th Apr 2017, 3:11 PM
Pim