I have the following code:
package pack1;
public class Father {
public String met1(){
return "c ";
}
protected String met2(){
return "b "+met1();
}
}
package pack2;
import pack1.Father;
class Child extends Father {
public String met1(){
return "a ";
}
}
class GrandChild extends Child {
public String met2(){
return super.met2() + "z" ;
}
public static void main(String[] args){
Father f = new GrandChild();
System.out.println(f.met2()); // Compile error here
}
}
When I call met2()
, it executes the version implemented in GrandChild
, that calls to super.met2()
, so it executed the version implemented in Child (that is the Father's version, because Child doesn't implements met2()
). But why does the compiler say "met2()
is not visible" if I'm calling it from GrandChild (subclass of Father, indirectly), when met2()
is "protected"? It should recognise met2()
because I'm calling it from a subclass and its protected (visible to subclasses)!!
Thank you!