0

I have class A in package one, and class B in package two that extends A and tries to override a protected method of A . I get a compile error method doIt() of A not visible. All this time I have assumed that a child class outside the parent class's package can access protected methods and fields.

package myFirstJava.models;

public class A {
    protected void doIt() {
        System.out.println("in A");
    }
}
package myFirstJava;

import myFirstJava.models.A;

public class Hello extends A {
    protected void doIt() {
        A a = new A();
        a.doIt(); // compile error method doIt() from A is not visible
        System.out.println(" in hello");
    }
}
khelwood
  • 52,115
  • 13
  • 74
  • 94
kobosh
  • 479
  • 2
  • 7
  • 23

0 Answers0