简体   繁体   English

为什么受保护的方法不可见?

[英]Why is the protected method not visible?

Java experts, I would sincerely appreciate any insights! Java专家,我将不胜感激!

I have an abstract class in a package with a protected method. 我在带有受保护方法的程序包中有一个抽象类。 I also have a subclass of this class in the same package. 我在同一包中也有此类的子类。 Now, when I try to instantiate the subclass from a class outside the package, and invoke the protected method on the subclass' instance, Eclipse is complaining the protected method is not visible. 现在,当我尝试从包外部的类实例化子类并在子类的实例上调用受保护的方法时,Eclipse抱怨受保护的方法不可见。

I thought, protected methods will be visible to all children - in or out of the package - as long as the class visibility does not restrict it - in this case, both the parent and the child class are public. 我认为,只要类的可见性不受限制,受保护的方法对所有子对象(包中或包外)都是可见的,在这种情况下,父类和子类都是公共的。 What am I missing? 我想念什么? Thanks in advance! 提前致谢!

package X;
public abstract class Transformation {
  protected OutputSet genOutputSet (List list) {
    ..
  }
}


package X;
public class LookupTransformation extends Transformation {
}


package Y;
import X.*;
public class Test {
  public static void main(String[] args) {
    List<field> fld_list = new ArrayList();
    ..
    LookupTransformation lkpCDC = new LookupTransformation();
    OutputSet o = lkpCDC.genOutputSet(fld_list); // Eclipse errors out here saying genOutputSet from the Type Transformation is not visible. WWWWWWWWHHHHHAAAATTTTTT????
  }
}


protected access means genOutputSet can be called by classes inheriting from the class where it's declared or by classes belonging to the same package. protected访问意味着genOutputSet可以由从声明该类的类继承的类或属于同一包的类来调用。 This means you can call it from within LookupTransformation . 这意味着您可以在LookupTransformation调用它。

However, you are trying to call it from an unrelated class - Test - located in a different package, which requires public access. 但是,你正在试图从一个不相关的类称之为- Test -位于不同的包,这需要public访问。

See additional explanation here . 请参阅此处的其他说明。

Your code is not in a subclass (you're in Test), and your code is not in the 您的代码不在子类中(您正在测试中),并且代码不在
same package (you're in Y). 相同的包裹(您在Y中)。 So the method is not visible. 因此该方法不可见。 That's normal. 那很正常

protected means you may call the method in any derived class. protected意味着您可以在任何派生类中调用该方法。 However, Test isn't derived from Transformation . 但是, Test不是从Transformation派生的。 genOutputSet is only visible inside Transformation and LookupTransformation . genOutputSet TransformationLookupTransformation 内部可见。 This doesn't tell anything about the visibility of methods when they are called on an object of the derived class. 当在派生类的对象上调用方法时,这并不能说明方法的可见性。

The best possible answer I could give would be in the form of this picture that I used to learn it myself: 我可能给出的最佳答案将是我自己用来学习的这张图片的形式:

在此处输入图片说明

Protected methods work on subclasses( inherited classes in your case) that are in other packages aswell. Protected方法也可用于其他packages子类(在您的情况下为inherited类)。 You are however calling it from a different class(not subclass ). 但是,您是从其他类(不是subclass )调用它的。 Hope this helps! 希望这可以帮助!

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

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