Method preference in Inheritance | Sololearn: Learn to code for FREE!

+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?

4/10/2017 2:41:05 PM

Pixie

1 Answer

New 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++.