简体   繁体   English

Java - 为什么另一个 package 中的子级无法通过父级引用访问父级的受保护方法?

[英]Java - Why Child in another package cannot access parent's protected method through parent reference?

I have two classes in two different packages:我在两个不同的包中有两个类:

package package1;

public class ParentClass {
    public void testPublic() {
    }

    protected void testProtected() {
    }
}


package package2;

import package1.ParentClass;

public class ChildClass extends ParentClass {
   void test() {
        ParentClass par = new ParentClass();
        par.testProtected(); // Line 1 : ERROR: testProtected() has protected access in ParentClass
        testProtected();     // Line 2 : No error

        ChildClass ch = new ChildClass();
        ch.testProtected(); // Line 3 : No ERROR
        testProtected();    // Line 4 : No error
    }    
}

I am able to understand why there is NO ERROR in calling testProtected() -- Line 2 since ChildClass sees this method as it inherits from ParentClass .我能够理解为什么在调用testProtected() -- Line 2 因为ChildClass看到此方法是从ParentClass继承的。

And somehow able to understand why it is throwing ERROR in calling par.testProtected() -- Line 1 , since par is a different object, and current object is not having access to other object's parent's protected method.并且以某种方式能够理解为什么它在调用par.testProtected() -- Line 1抛出错误- 第 1 行,因为par是不同的 object,并且当前 object 无法访问其他对象的父级受保护方法。

But how it is possible for an object of ChildClass to access this same method ch.testProtected() -- Line 3 (other object's parent's protected method) when the reference type is of ChildClass only?但是,当引用类型仅为 ChildClass 时,ChildClass 的 object 如何访问相同的方法ch.testProtected() -- Line 3 (其他对象的父对象的受保护方法)?

The error you're seeing makes total sense.您看到的错误完全有道理。

When you do the following当您执行以下操作时

ParentClass par = new ParentClass();
par.testProtected();

you're trying to access testProtected as if it were part of the public API of ParentClass , that's it, you're calling from another package a protected method of ParentClass on an instance of that class .您正在尝试访问testProtected ,就好像它是ParentClass的公共 API 的一部分一样,就是这样,您正在从另一个 package调用该 ZA2F2ED4F8EBC2CBB4C21A 实例上ParentClass的受保护方法。 It doesn't matter it is being called inside an extending class, all that matters is that it's being called from another package.在扩展的 class 中调用它并不重要,重要的是它是从另一个 package 调用的。

Now, if you do现在,如果你这样做

ChildClass ch = new ChildClass();
ch.testProtected(); // Line 3
testProtected();    // Line 4

you will not see any errors.你不会看到任何错误。 Why?为什么?

In line 3 you're accessing a method upon an instance of class ChildClass inside ChildClass , so that's legal.在第 3 行中,您正在访问ChildClass内 class ChildClass实例的方法,因此这是合法的。 Even if you would have had a private method childMethod in ChildClass you could have done ch.childMethod() .即使您在ChildClass中有一个私有方法childMethod ,您也可以完成ch.childMethod()

In line 4 you're accessing testProtected through the this reference (implicitly), it means it calls the method through the ChildClass , which, again is legal.在第 4 行中,您正在通过this引用(隐式)访问testProtected ,这意味着它通过ChildClass调用该方法,这也是合法的。

See this related post: What is the difference between public, protected, package-private and private in Java?请参阅此相关帖子: Java 中的 public、protected、package-private 和 private 有什么区别? (hint: last row in the described table) (提示:所述表中的最后一行)

暂无
暂无

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

相关问题 无法使用Java访问父级的受保护方法 - Cannot access parent's protected method in Java 为什么另一个 package 中的子类无法访问受保护的方法? - Why subclass in another package cannot access a protected method? Java访问不同包中子类中的受保护成员,使用父类型的对象引用 - Java access to protected member in subclass in different package, using object reference of parent type 为什么我无法使用父类型访问子对象方法 - Why I cannot access Child Object method with Parent Type 当父级位于不同的程序包中且子类为Abstract时,模拟受保护的父级方法 - Mock Protected Parent Method When the Parent is in a Different Package and the Child Class is Abstract 当父类是引用类型并通过方法传递时,如何从子类访问属性? - How can I access an attribute from a child class when the parent class is the reference type and passed through a method? 为什么引用子类对象的父类类型引用变量无法访问子类的方法 - Why parent class type reference variable having reference to child class object can't access child class's Methods 当父级位于其他程序包中时,模拟受保护的父级方法 - Mock Protected Parent Method When the Parent is in a Different Package 为什么不能使用父引用访问子字段 - why can not access child fields using parent reference 为什么在父类中必须强制有一个子方法以引用父类的引用来调用它,该父类引用子类的对象? - Why is it mandatory to have a child method in Parent class to call it with a reference of parent class which is referring to an object of child class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM