0

I have an abstract base class A and I have some concrete classes B and C extending A and implementing all abstract methods.

abstract class A {...}
class B extends A {...}
class C extends A {...}

Now I would like to add some functionality to A, but I can not touch A, B and C. So I thought it might be possible to write a generic class D extending any of the concrete classes B or C.

class D<T> extends <T extends A> {...}

Is it possible? And if so what is the correct syntax?

ceving
  • 21,900
  • 13
  • 104
  • 178
  • 16
    Composition instead of inheritance. Every day, all day. – ppeterka Sep 03 '13 at 15:54
  • 4
    @ppeterka Don't ever change your rep. – Sotirios Delimanolis Sep 03 '13 at 15:54
  • @ppeterka. Nice reputation you have. – Rohit Jain Sep 03 '13 at 15:54
  • @RohitJain I'm afraid to write answers now, and have it screen captured :) (insert evil smiley here) (BTW, check it now) – ppeterka Sep 03 '13 at 15:55
  • @ppeterka. hehe. nice :) – Rohit Jain Sep 03 '13 at 16:01
  • @ppeterka Composition means that I have to duplicate all child classes. The example shows only 2 but I have *many*. This means thousands of composition methods `doit (args) {return other.doit(args);}`. I would like to avoid this redundant code mess. – ceving Sep 03 '13 at 16:02
  • @ppeterka66 composition instead of concrete inheritance – nachokk Sep 03 '13 at 16:03
  • @ceving is the hierarchy deeper, or are there only 2 levels? (A, as superclass, and B, C, D, etc as child classes?) However, if we are talking about getting literally thousands of wrapper methods, either I did misunderstand something, or there is a chance that something is very bad about this hierarchy... – ppeterka Sep 03 '13 at 16:06
  • @ppeterka66 only 2 levels as in my example – ceving Sep 03 '13 at 16:09
  • @ceving Then if I see it well, you'd have to have an interface for A's exposed public and protected methods, and only 1 (abstract) wrapper class for the composition part, implementing the interface. Then B, C... should extend the wrapper class, and add their own functionality without each having to have all the methods replicated. With this, as long as you don't touch the super interface, B, C, and the others can be left the same, and you can create a new interface, which class A can implement, adding new functionality. If I understood the issue correctly. Without refactoring? Possibly no... – ppeterka Sep 03 '13 at 16:14
  • @ppeterka66 Sorry I did not mention that the children B and C add child specific functionality to the base class A. So it is not enough to care about the methods of the abstract base class. – ceving Sep 03 '13 at 16:18
  • @ceving I see... That's tough. To make matters worse, you could use Proxies, or runtime bytecode generation... But I would literally hurt someone doing such an evil thing (check reps) without a **real heavy**, and supported cause, but I see this is already way off topic since the word Proxy came up... – ppeterka Sep 03 '13 at 16:22

0 Answers0