0

ClassA has two children : ClassB and ClassC.

We need to override a method of ClassA but we will be using ClassB and ClassC (with the modified method).

ClassA, ClassB and ClassC are all part of an external library which we can not modify.

Is it possible to get this done without subclassing ClassB and ClassC seperately using the same re-implementation of the method in each subclass?

Would the problem be different if ClassB already had a re-implementation of the method that we wanted to ignore?

Jos Vinke
  • 2,704
  • 3
  • 26
  • 45
danjjl
  • 432
  • 1
  • 5
  • 16
  • Here's a similar question in the specific, somewhat related case, of protocols http://stackoverflow.com/questions/32798005 – Fattie Sep 26 '15 at 14:24

2 Answers2

1

Unless there is some special mechanism in play that somehow allows you to do this, it is not possible. It would also make no difference if ClassB had its own overridden implementation.

Since ClassB and ClassC have already been compiled, the implementation of their methods has already been baked into your external library. In general, there is nothing you can do at source code level to change this unless you have your own derived classes.

If we are talking about a managed language with a runtime that allows you to dynamically modify the call targets, or if a hot-patching mechanism that can rewrite machine code on the fly is in play then you might have a way out -- but that would be a special exception and not the rule.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

It sorta depends on the language, and it sounds like a hack at best. If you are writing this in Javascript, you can directly modify the prototype function of class B and C.

In most C-family of languages though, I don't think this is possible. Multiple inheritance may lead to ambiguities (I imagine B and C each have their own subclassing), so you'd probably have to subclass each.

CDC
  • 51
  • 5