I\'d like to do the following:
public class Sub extends Super {
public Sub(Super underlying) {
if (underlying == null) {
underlying = super; // this
I think something like what you want can be done if you are willing to add, and call, subclass-specific method names for the methods involved:
public class Test {
public static void main(String[] args) {
Super sup = new Super();
Sub sub1 = new Sub(null);
Sub sub2 = new Sub(sup);
sub1.subMethod();
sub2.subMethod();
sup.method();
}
}
class Super {
public void method(){
System.out.println("Using method from "+this);
}
}
class Sub extends Super {
private Super underlying;
public Sub(Super underlying) {
if (underlying == null) {
underlying = this;
}
this.underlying = underlying;
}
public void subMethod() {
underlying.method();
}
}