Probably related to #354, when overriding methods with an implementation in a base class, from a reference of type base class, the wrong implementations get called.
OK, so overriding private functions is illegal according to the spec, but since the compiler doesn't complain, I included those to show the similarity between package and private functions (same error with both).
// Declare and define some methods...
class A {
private void iPriv () { assert(false, "iPriv"); }
protected void iProt () { assert(false, "iProt"); }
package void iPack () { assert(false, "iPack"); }
public void iPub () { assert(false, "iPub"); }
}
// ... but override them all here:
class B : A {
private override void iPriv() {} // (lesser issue: shouldn't the compiler complain here?)
protected override void iProt() {}
package override void iPack() {}
public override void iPub() {}
}
void main() {
// None of these assert of course (overriden functions are called):
B b = new B;
b.iPriv;
b.iProt;
b.iPack;
b.iPub;
// But when we call from a base class with an implementation:
A a = b;
a.iPriv; // This calls A.iPriv and asserts
a.iProt;
a.iPack; // This calls A.iPack and asserts
a.iPub;
}
Unless the a.iPriv; and a.iPack; calls are removed, the program asserts when run.
- With private functions this is nearly OK, but the compiler should complain about trying to override them instead of silently calling the base implementation.
- With package protection, the wrong implementation of iPack() is called.