use object as next object super's
Is there a clear way on the next puzzle: Can you use an object of Class A
as the super of a new object of Class B without knowing any of the
structure of Class A?
Class A {
private String a;
public A(String a){this.a=a;}
public String getA(){return a;}
}
Class B extends A{
private String b;
public B(String b){this.b=b;}
public String getB(){return b;}
}
A a = new A("somethingA");
B b = new B("somethingB");
b.setSuper(a);
In this case it would be obvious to create a new method setA(String a) in
class A. But since class A could become much more complex, isn't there a
convenient way of initializing class B and setting the super class with an
already existing class A object?
No comments:
Post a Comment