简体   繁体   English

使用子类和超类引用访问实例方法

[英]Accessing instance methods using subclass and superclass references

Here is the sample code:这是示例代码:

package com.ankur.javapractice;
public class SuperClass {

    public void someMethod(int val) {
        System.out.println("Public method in SuperClass");
    }
}

class SubClass extends SuperClass {

    public void someMethod() {
        System.out.println("Public method in SubClass");
    }

    public static void main(String[] args) throws InterruptedException {
        SuperClass superClassRef = new SubClass();
        SubClass subClassRef = new SubClass();
        //superClassRef.someMethod(); // (1)
        subClassRef.someMethod(8); // (2)
    }
}

I understand why (1) won't work at compile time (that's why I have it commented out).我理解为什么 (1)在编译时不起作用(这就是我将其注释掉的原因)。 It won't work because someMethod() is not known to superClassRef since it is of type SuperClass and SuperClass does not define someMethod() .它不起作用,因为superClassRef不知道someMethod() ,因为它是SuperClass类型并且SuperClass没有定义someMethod()

My question is why/how does (2) work then at compile time?我的问题是(2)为什么/如何在编译时工作? Using the same explanation as above, at compile time , someMethod(int) is also not known to subClassRef since it is of type SubClass and SubClass does not define someMethod(int) .使用与上面相同的解释,在编译时subClassRef也不知道someMethod(int) ,因为它是SubClass类型并且SubClass没有定义someMethod(int)

Edit and Update:编辑和更新:

Some answers pointed out that SubClass inherits someMethod(int) from SuperClass .一些答案指出SubClassSuperClass继承someMethod(int) That is correct and I understand that;这是正确的,我理解; my follow up question to that is how is compiler able to take inheritence relationships into account?我的后续问题是编译器如何考虑继承关系?

Yes someMethod(int) is defined in parent class which is SuperClass , but when child class inherits parents class using extends keyword then subclass inherits all of the public and protected members of its parent and can use them directly, which is called Inheritance Yes someMethod(int) is defined in parent class which is SuperClass , but when child class inherits parents class using extends keyword then subclass inherits all of the public and protected members of its parent and can use them directly, which is called Inheritance

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.子类继承其父类的所有公共和受保护成员,无论子类在哪个 package 中。如果子类与其父类在同一个 package 中,它还继承父类的包私有成员。 You can use the inherited members as is, replace them, hide them, or supplement them with new members:您可以按原样使用继承的成员、替换它们、隐藏它们或用新成员补充它们:

  • The inherited methods can be used directly as they are.继承的方法可以直接使用。

This is because SubClass inherits someMethod(int) and has someMethod(), so it has two different methods.这是因为 SubClass 继承了 someMethod(int) 并且有 someMethod(),所以它有两个不同的方法。 SuperClass cannot access someMethod() because it is not extending any classes. SuperClass 无法访问 someMethod() 因为它没有扩展任何类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM