Search code examples
javarefactoringbinary-compatibility

Does removing an interface break code calling methods on the object?


I need to do some refactoring in Java, and I need to maintain some degree of binary compatibility. In this case I want to remove some legacy interfaces, that are not used anywhere anymore and which require a rather big (and also deprecated) external dependency.

I have class C that implements interface I, and I have code that calls a method (declared in the interface) on an instance of C. The calling code knows that it is using C, not just the interface.

 class C implements I {
      void theMethod(){} ; // is declared in the interface I
 }

 C object;
 object.theMethod();

When I remove the interface from the class definition (but keep all the methods), will the calling code (which does not refer to the interface at all) still work (without a recompile)?


Solution

  • Yes, it will work - as long as it doesn't explicitly refer to interface I anywhere.

    From JLS: Resolution of Symbolic References:

    The binary representation of a class or interface references other classes and interfaces and their fields, methods, and constructors symbolically, using the binary names (§13.1) of the other classes and interfaces

    Class ClientClass referring to field / method of class C contains no implicit references to interface I that class may implement.