简体   繁体   English

为什么不能从Java中另一个包的继承类调用受保护的方法?

[英]Why can't I call a protected method from an inheriting class in another package in Java?

Say there's the following base class: 假设存在以下基类:

package bg.svetlin.ui.controls;

public abstract class Control {
    protected int getHeight() {
        //..
    }
    //...
}

Also, in the same package, there's a class that inherits: 同样,在同一个程序包中,有一个继承的类:

package bg.svetlin.ui.controls;

public abstract class LayoutControl extends Control {
    public abstract void addControl(Control control);
    //...
}

Then, there's a third class in another package: 然后,另一个包中有第三类:

package bg.svetlin.ui.controls.screen;

public abstract class Screen extends LayoutControl {
    //...
}

And, finally, there's the implementation class, again in a different package: 最后,还有实现类,同样在另一个包中:

package bg.svetlin.ui.controls.screen.list;    

public class List extends Screen {

    private final Vector controls = new Vector();

    public void addControl(Control control) {
        height += control.getHeight();
        controls.addElement(control);
    }
}

Even though List inherits from Control , and the getHeight() is protected , there's the following error: 即使List继承自Control ,并且getHeight() protected ,也存在以下错误:

getHeight() has protected access in bg.svetlin.ui.controls.Control getHeight()在bg.svetlin.ui.controls.Control中已保护访问

I've checked that my imports are right. 我检查了我的进口货物是否正确。 I'm using NetBeans. 我正在使用NetBeans。

Any idea what's wrong? 知道有什么问题吗? I thought protected fields and methods are visible to the children even if the latter are in a different package. 我认为protected字段和方法对于孩子来说是可见的,即使后者位于不同的包装中。

Thanks! 谢谢!

I thought protected fields and methods are visible to the children even if the latter are in a different package. 我认为受保护的字段和方法对于孩子来说是可见的,即使后者在其他程序包中也是如此。

That's correct. 没错 The class itself has an access to the inherited protected members. 该类本身可以访问继承的受保护成员。 But, what you're trying to do it to call the getHeight method on some Control reference. 但是,您要尝试在某个 Control参考上调用getHeight方法。 You're allowed to call it only on this instance! 您只能在实例上调用它!

For a better understanding, let me quote Kathy Sierra's SCJP Preparation Guide : 为了更好地理解,请允许我引用Kathy Sierra的SCJP准备指南

But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? 但是,包的子类可以访问超类(父)成员是什么意思? It means the subclass inherits the member. 这意味着子类继承成员。 It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. 但是,这并不意味着包外部子类可以使用对超类实例的引用来访问成员。 In other words, protected = inheritance. 换句话说,保护=继承。 The subclass can see the protected member only through inheritance . 子类只能通过继承看到受保护的成员

You're right. 你是对的。 Any protected member or method accessible from children class, but you want access to protected method of a parameter instance in addControl method. 可从子类访问的任何protected成员或method ,但您希望在addControl方法中访问参数实例的受保护方法。 You can access only to protected method of List class ( this.getHeight() ) 您只能访问List类的protected方法( this.getHeight()

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

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