Nice way to override method in superclass, so that it's used in multiple subclasses?
I'm working with this library JFreeChart, though the question has nothing to do with it, just general Java programming. I'm fixing it up a little overriding methods in the middle of the class-hierarchy, for example I've changed methods in NumberAxis which is extended by both DateAxis and SymbolAxis.
Is there a "nice" way to use these extended methods in both DateAxis and SymbolAxis without re-compiling the whole thing (or really just NumberAxis I suppose)? It feels like there should be some pattern for this as it doesn't seem too unusual?
I suppose I could do something like this
Code :
class A { ... }
class B extends A { ... }
class C extends A { ... }
class D extends A {
private A BorC = null;
public D(A BorC) {
this.BorC = BorC;
}
protected void overriddenAfunction() { ... }
protected void overriddenBfunction() {
((B) BorC).overriddenBfunction();
}
protected void overriddenCfunction() {
((C) BorC).overriddenCfunction();
}
}
Usage:
A testB = new D(new B());
A testC = new D(new C());
However if it even works I need to put in every function from B and C and call them from D.
Now I've just extended SymbolAxis and overridden the method from NumberAxis there.