Calling overloaded inherited methods using super class reference

后端 未结 10 1308
既然无缘
既然无缘 2020-12-29 07:38

I do not understand this Java behavior. I have two classes:

class C1 {
    public void m1(double num) {
        System.out.println(\"Inside C1.m1(): \" + num         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 08:41

    You cannot see methods of C2, because you instance variable is declared as C1 and also because they don't have the same signature. You have double parameter in one method and in second a int type which makes them for JVM completely different methods (so no inheritance will work here).

    So if you have int type in C1 method then you need to have also int type in C2 method, then JVM will run method from C2 like you wanted.

    Also you can cast variable to C2 type then you will be able to access to methods of C2.

提交回复
热议问题