I have Tadpole
class that extends Frog
, as follows:
1: package animal;
2: public class Frog {
3: protected void ribbit() { }
4: void jump() { }
5: }
1: package other;
2: import animal.*;
3: public class Tadpole extends Frog {
4: public static void main(String[] args) {
5: Tadpole t = new Tadpole();
6: t.ribbit();
7: t.jump(); // doesn't compile due to default access
8: Frog f = new Tadpole();
9: f.ribbit(); // doesn't compile
10: f.jump(); // doesn't compile due to default access
11: } }
Lines 7 and 10 do not compile due to the package-private access of the jump() method.
I understand that upcasting of Tadpole
class into Frog
class on line 8 will allow the parent instance member (the ribbit method) to be accessed from the subclass since the method is not overwritten.
Protected methods should be able to be accessed from a subclass whether it's in the same package or different package right? If so, why doesn't line 9 compile?