Assumed, we have given the two classes:
package inheritance1;
public class Foo1 {
protected void sayWhoYouAre() {
System.out.println("I am Foo1");
}
}
and
package inheritance2;
import inheritance1.Foo1;
public class Foo2 extends Foo1 {
public Foo2() {
// why does this not work?
// new Foo1().sayWhoYouAre();
// ---> Error: "The method sayWhoYouAre() from the type Foo1 is not
// visible"
}
@Override
protected void sayWhoYouAre() {
System.out.println("I am Foo2");
}
public static void main(String[] args) {
// why does this not work?
// new Foo1().sayWhoYouAre();
// ---> Error: "The method sayWhoYouAre() from the type Foo1 is not
// visible"
}
}
Why is the sayWhoYourAre()
-method visible in the class definition so I can override it (@Override
), but when I try to invoke it by new Foo1().sayWhoYouAre()
the compiler says, that this method is not visible?
Thanks for your help! :)