我有两种不同的封装以下两类。 我的实例方法访问修饰符是受保护的,这意味着在相同或不同的封装任何子类具有对它的访问权? 然而,在Eclipse中我看到下面的消息在我的子类Cat
上线17
The method testInstanceMethod() from the type Animal is not visible
我对超和子类代码如下。
package inheritance;
public class Animal {
public static void testClassMethod() {
System.out.println("The class" + " method in Animal.");
}
protected void testInstanceMethod() {
System.out.println("The instance " + " method in Animal.");
}
}
package testpackage;
import inheritance.Animal;
public class Cat extends Animal{
public static void testClassMethod() {
System.out.println("The class method" + " in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
myAnimal.testClassMethod();
myAnimal.testInstanceMethod();
}
}