Why is it that when I create a reference of the super class in the sub-classs that only methods that are public can be called from the reference and not methods that are protected. (The classes are in different packages)
package pet;
public class Dog {
protected void bark(){};
void jump(){};
public void lick(){};
}
package other;
import pet.*;
public class Husky extends Dog {
public static void main(String[] args){
Husky h = new Husky();
h.bark(); //COMPILES (Husky is a subclass of Dog - Protected method)
h.jump(); //DOES NOT COMPILE (Different packages - package-private access method)
Dog d = new Dog();
d.bark(); //DOES NOT COMPILE WHY?
d.jump(); //DOES NOT COMPILE (Different packages - package-private access method)
d.lick(); //COMPILES (Method is public)
}
}
My question is why doesn't d.bark() compile? The bark method has an access modifier of protected, which allows it to be accessed from classes in the same package or subclasses. So what's going on?
If the husky reference is able to access the bark method, by the same logic the dog reference should also be able to access the bark method.
So I can only assume that there must be an issue with the Dog reference?