in java how to extend an implementation of interafce?
I have an interface an开发者_运维问答d 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 A
at runtime, you can use proxy classes to create an object that implements all the interfaces that the class implements, including X
, and that overrides some methods of X
to 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.
Here's your interface:
interface X{ void methodA(int); int methodB();}
Make an abstract super class:
abstract class A implements X { /*impl code here*/}
Now B (and every other class that extends A) promises to implement the interface:
class B extends A {/*other impl here*/}
Is there a reason you want to avoid composition here? You aren't going to have access to the mystery implementation without the name. You can't extend ? extends X
, but you can have an instance of it. This is rather trivial using composition:
interface X
{
void methodA( int param );
int methodB();
}
class YourImpl<T extends X> implements X
{
private T delegate;
public YourImpl( T delegate )
{
this.delegate = delegate;
}
void methodA( int param )
{
delegate.methodA( param );
}
int methodB()
{
return delegate.methodB();
}
}
You don't stand to gain much in this example, but if you do additional processing before/after delegation, there's a lot to be gained.
Generally speaking, people gravitate towards inheritance to solve their problems, even when composition is more appropriate. I would suggest reconsidering whether composition is an okay solution for you. Another thing to consider is whether the mystery implementation can use your implementation, rather than vice versa.
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.
That is not possible. Java does not allow you to extend
a generic type parameter. You can only extend
a named class.
To achieve what you are trying to achieve, you will need to use some kind of proxying, delegation or wrappering. For example:
public class B implements X {
private X x;
public B(X x) {
this.x = x;
}
// delegate each method in the X interface as required; e.g.
public int someMethod(String s) {
return x.someMethod(s);
}
// add any implementations of B-specific methods.
}
This is not exactly "composition", but I suspect it is the kind of thing you are trying to avoid. Unfortunately (for you), there's no real alternative. The Java language and the JVM both require that every class apart from Object has a single definite super-class.
精彩评论