I am using 2 libraries and I need a class from one of the libraries to have functionality from the other. My first try was to take the source of library A and make the class extend the class from library B. This works kinda fine except alot of fixing etc which don’t make the code all that stable etc and updating one of the libraries became a hell.
like this:
abstract class classA extends classB{
}
Now I also thought about doing something like this:
abstract class newClass extends classB{
private classA A = new classA();
public void exampleMethod(){
A.exampleMethod();
}
}
Is the second option bad performance-wise? and if it is, how bad?
The extension through delegation form (mixin) is desirable for a variety of reasons here, but the most pertinent is that you are (likely) extending 3rd party library classes in a non-normative manner.
But this is all guessing since we have no idea what libraries you are talking about. For example, it may be that it is a poorly written library where
classA(using your example) really is not supposed to be extended but the author neglected to specfinal class classA, etc. If that were the case, then clearly you do not want to extend that class (even if you can).All that said, given the fact that you (a) only need shared functionality, (b) do not require polymorphic semantics, the delegated form makes perfect sense.
Advice regarding performance anxiety: benchmark it.
And don’t neglect final — JIT is your friend (be nice to it and feed it):