This is all in the context of JBox2D (in Java). The World class creates Body instances with a function. I’m trying to add a bit more stuff to Body for my application. Body is represented by ComplexClassWithLotsOfAPI in this question.
Here is the generalized question. I am trying to add a bit more functionality to a premade class by extending the class. I hope to do something like this:
class SomeMore extends ComplexClassWithLotsOfAPI{
int myExtraInt;
//A bit more API functions
}
So that I may do this:
SomeMore sm=new SomeMore();
sm.someOldAPI();
sm.butAlsoMyNewAPI();
The problem is that this ComplexClassWithLotsOfAPI is created by another class that I can’t modify (the World class in the original context), so I am not simply creating them on my own (otherwise this would work). Since I am stuck having to start with a ComplexClassWithLotsOfAPI, I have been searching for a way to construct a SubClass from a SuperClass, whereas there are many examples of casting a SuperClass to a Subclass (but this is not applicable here). Here is an example of the function that needs to be completed:
public SomeMore create(...){
ComplexClassWithLotsOfAPI ccwlao=myWorld.create(...);
SomeMore sm;
//??
return sm;
}
Alternative to Wrapping?
My original solution was to encase the ComplexClassWithLotsOfAPI into my own class. In order to construct my new class, I simply pass the old class into my new constructor and move on:
class SomeMore{
public ComplexClassWithLotsOfAPI ccwloa;
int myExtraInt;
public SomeMore(ComplexClassWithLotsOfAPI nccwloa){
ccwloa=nccwloa;
myExtraInt=0;
}
//A bit more API functions
}
public SomeMore create(...){
ComplexClassWithLotsOfAPI ccwlao=myWorld.create(...);
SomeMore sm=new SomeMore(ccwlao);
return sm;
//OR more simply
//return new SomeMore(myWorld.create(...));
}
But in order to access the old API, I need to do this:
SomeMore sm=new SomeMore();
sm.ccwloa.someOldAPI();
sm.butAlsoMyNewAPI();
I could just be a bit unreasonable, but this kind of functionality is tedious and adds just that much more complication to something that doesn’t need it. I mean, if someone wanted to add a tad more functionality, would they wrap my class into yet another class, and have 3 class heirarchies to go through to get old APIs? Also, it would feel just wasteful to wrap every single API in the old class into my new class (there’s a lot of them).
sm.someOldAPIButWrappedInMyClass(); //not desirable
I do not have access to the java files of ComplexClassWithLotsOfAPI, only the compiled class files. I can not simply force my modifications into the old class (and even if I could, I’d prefer not to anyway). I am relatively new to java, so perhaps this is not the best/proper way to do this, but I haven’t been able to find an alternative.
Eclipse can build you a delegate class that is a subclass of some class (i.e. Parent) and holds an instance of ‘Parent’ in a field (called the delegatee) and generates methods that override all the methods in ‘Parent’ with calls to the same method in the delegatee. You can then add your own methods.
You do this from the context menu, Source option, generate delegate methods. You have to have the subclass and have it extend ‘Parent’ and have a field of type ‘Parent’ to let the code generator work.
Here’s an example: