I have an interface and I am promised by someone to provide its implementation.
I have to now design a class s.t I can extend “someone’s” implementation. However I do not know the name of that implementation.
eg.
interface X{ void methodA(int); int methodB();}
class A implements X { /*impl code here*/}
Now, I don’t know , what class is going to implement X. But I want to incorporate those implementations into my code
class B extends <Anyclass that implements interface X>
{/*other impl here*/}
How do I go about doing this? I know in generics you can specify when using Comparator. I want to know if there is anything available for this case?
So you want an instance of X that has the same interface as the class that you get at runtime, but allows you to specify how certain methods of X behave?
If you have an instance of
Aat runtime, you can use proxy classes to create an object that implements all the interfaces that the class implements, includingX, and that overrides some methods ofXto do what you want, and delegates the rest to the instance.Proxy classes are not great efficiency wise and the kind of dynamism they provide is usually easier to achieve by more normal means, so don’t overuse them, but where you need a bit of flexible glue at the boundary between two parts of a system, they can really help.
Also note, that the resulting object is not an A, it just implements the same interfaces as A.