Java Reference
In-Depth Information
package P3;
import P1.Concrete2;
public class Concrete3 extends Concrete2 {
public void pri() { print("Concrete3.pri()"); }
public void pac() { print("Concrete3.pac()"); }
public void pro() { print("Concrete3.pro()"); }
public void pub() { print("Concrete3.pub()"); }
}
Invoking
show
on a
Concrete3
object prints
AbstractBase.pri()
Concrete3.pac()
Concrete3.pro()
Concrete3.pub()
Here the method
Concrete3.pac
appears to have overridden the inaccess-
ible
AbstractBase.pac
. In fact,
Concrete3.pac
overrides
Concrete2.pac
, and
Concrete2.pac
overrides
AbstractBase.pac
therefore
Concrete3.pac
transit-
ively overrides
AbstractBase.pac
. By redeclaring
pac
as public,
Concrete2
made it accessible and overridable by any subclass.
[3]
[3]
This illustrates why weaving in and out of a package can be confusing and should be avoided.