Why parent class method is getting called and not child class in this case?

前端 未结 4 964
长情又很酷
长情又很酷 2021-01-20 06:16

I have a parent class A, and its child B. Both are having doSomething method with diff type of parameters.

Class A

package Inheritance;
         


        
4条回答
  •  时光取名叫无心
    2021-01-20 06:41

    When you are using a reference of type A, you see only the methods defined for class A. Since doSomething in B doesn't override doSomething in A (since it has a different signature), it is not called.

    If you were to use a reference of type B, both methods would be available, and doSomething of B would be chosen, since it has a more specific argument (String vs Object).

提交回复
热议问题