I have a Java interface in an API that contains a single 开发者_运维知识库method:
public interface MyInterface
{
public void foo();
}
If I add a method to that interface like this:
public interface MyInterface
{
public void foo();
public void bar();
}
Do clients using the API need to recompile or can they use the new JAR as is because I didn't change the signature or an existing method or remove a method?
Short answer: The can use the new API without recompiling.
Here's a LINK that might help you understand how Java runs bytecode and how it does dynamic linking.
It depends.
If the clients are going to implement the interface, the clients will not only have to recompile but they will have to first implement the new method in their implementing class(es)
If the clients are going to only use the interface (as reference to objects created on your side of the system) clients wont have to recompile.
If you add a method, the implementer doesn't need to be recompiled if the method is never used. Why would you do this? If you are implementing a third party interface e.g. JDBC or JMS and methods are added which you neither implement nor call, you don't need to recompile.
The caller doesn't need to be recompiled if the method is not overloaded. However if the method is overloaded, the caller may need to be recompiled as it might use a different method call.
interface Methods {
void method(double d);
// adding a method
void method(int i);
}
// calling code
Method methods = ...
methods.method(1);
Without recompiling, the caller will continue to call the first method, however after re-compiling, the caller will call the second method.
If you remove a method which is not called, this is fine also.
Renaming a method is the same as removing the old one and adding a new one.
If you change the parameter type OR the return type of a method which is used, you will need to recompile.
精彩评论