As the explanation states C1 will inherit m1 from B1 which in turn inherit from A.
This is where I am confused and please clarify.
Since C1 and B1 inherits m1() from A. Doesn't this mean that C1 and B1 have their own m1() as a result of inheritance? Why then does it says it will call A's m1()?
B1 extends A therefore B inherits m1() from A. It doesn't mean there are two versions of m1 now. There is still only one m1 and that is in A. B just inherits it.
Similarly, C1 extends from B1 and so C1 inherits everything that B has got, which includes m1. So when you call m1 on an object of class C1, it is the m1 defined in class A that is invoked.
If you like our products and services, please help us by posting your review here.
Option 1 is the opposite of options 3. So if you understand why option 3 is correct, how do you think option 1 can be correct?
Also, the explanation to option 1 clearly explains why it will NOT generate a compilation error, "C1 will inherit B1's m1() which in turn inherits m1() from A."
If you like our products and services, please help us by posting your review here.
class A{
public void m1(){ }
}
class B1 extends A{
}
class B2 extends A{
public void m1(){ }
}
class C1 extends B1{
}
class C2 extends B1{
public void m1(){ }
}
thank you for your feedback!
If you like our products and services, please help us by posting your review here.