1

I know the code below doesn't compile, and yes I might be crazy. What I'm trying to do is slip an implementation (B below) of a abstract class (A) as the super class of class (C) that also extends the abstract class. I'd also like to defer method override to the super class. Something like...

abstract class A { abstract Object get(); abstract Object do(); Object action(); } class B extends A { B(B obj) { super(obj.getName()...) } Object get() { ... } Object do() { ... } } C<T> extends <T extends A> { C(<T extends A> obj) { super(obj) } Object do() { ... } Object whatever() { return action(); } } 

Where...

C aC = new aC(new B()); aC.get() -> B.get() aC.do() -> C.do() aC.action() -> A.action() 
2
  • No, you cannot dynamically define your class structure like that in Java. If you define an interface, you could use composition to accomplish this, together with a dynamic proxy. But that relies on reflection, which may or may not be a problem. Commented Nov 17, 2009 at 4:34
  • Yep, I considered this. That's a bit involved, especially since generics is right there. I'll see if anything like this is upcoming. Thanks. Commented Nov 17, 2009 at 23:59

1 Answer 1

1

I think you want to use the delegate pattern.

class C extends A { private final A delegate; C(A obj) { this.delegate = obj; } Object get() { return delegate.get() } Object whatever() { return action(); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, this is what I ended up doing. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.