在Java中,有没有关于何时使用每个访问修饰符,即默认明确的规则(包私有)public,protected并且private,同时使class与interface和处理继承?
我从子类调用超类'受保护的方法.为什么这种方法"不可见"?
我一直在读一些职位如这一个,这似乎违背了以下内容:
超级课程:
package com.first;
public class Base
{
protected void sayHello()
{
System.out.println("hi!");
}
}
Run Code Online (Sandbox Code Playgroud)
子类:
package com.second;
import com.first.Base;
public class BaseChild extends Base
{
Base base = new Base();
@Override
protected void sayHello()
{
super.sayHello(); //OK :)
base.sayHello(); //Hmmm... "The method sayHello() from the type Base is not visible" ?!?
}
}
Run Code Online (Sandbox Code Playgroud)