【发布时间】:2014-04-20 02:54:42
【问题描述】:
我正在阅读 Java 中的 protected 修饰符,哪些字段可以在同一个包和子类中访问。
现在我已经写了一些代码。
package com;
public class Parent {
protected void print()
{
System.out.println("dFDF");
}
}
现在是子类。
package abstraact.concept;
import com.Parent;
public class BaseParent extends Parent{
public void printNum()
{
Parent p = new Parent();
p.print(); /** Getting error here */
// The method print() from the type Parent is not visible
}
public static void main(String[] args) {
BaseParent pp = new BaseParent();
pp.printNum();
}
}
为什么我会出错? 由于受保护的方法/变量可以从子类访问。
【问题讨论】:
-
显示方法在哪里??
标签: java inheritance subclass protected