I’m using this library and I’ve had to override some methods in a few super-classes. What I do now is extending the class I need and re-define the methods I need to override from it’s super-class.
class A { ... }
class B extends A { ... }
class C extends A { ... }
class myD extends B {
protected void overriddenAfunction() { ... }
}
This obviously isn’t good when several classes extends the same super-class and I want all the sub-classes to use the same overriden methods (like if C would also need the same definition of overridenAFunction()).
Is there some design pattern or way to make this pretty? I’m imagining you could do something like this:
class A { ... }
class B extends A { ... }
class C extends A { ... }
class myD extends A {
private A BorC = null;
public myD(A BorC) {
this.BorC = BorC;
}
protected void overriddenAfunction() { ... }
public void overriddenBfunction() {
((B) BorC).overriddenBfunction();
}
public void overriddenCfunction() {
((C) BorC).overriddenCfunction();
}
}
Usage:
A testB = new myD(new B());
A testC = new myD(new C());
However then I’d need to put in every method from B and C if it even works.
Is there a better way or workaround? All I want to do is avoid re-packaging the library though I guess it’s really the best solution?
I guess I could also make copies of B and C and have them extend myD while myD extends A. This library also have all the source-files available so I can do this or something similar.
There is no way to override
Ain a way that letsBandCbenefit from the new functionality. You only have two options that I can see, and you mention them both:Aand repackage the libraryMyDextendA, and then Make copies ofBandCthat extendMyDinstead.