Sam Sam - 29 days ago 22
Java Question

What is binary compatibility

I was reading Effective Java by Joshua Bloch.

In Item 17: "Use interfaces only to define types", I came across the explanation where it is not advised to use Interfaces for storing constants. I am putting the explanation below.

"Worse, it represents a commitment: if in a future release the class is modified so that it
no longer needs to use the constants, it still must implement the interface to ensure binary
compatibility."


What does binary compatibility mean here?

Can someone guide me with an example in Java to show that code is binary compatible.

Answer

In short, binary compatibility means that when you change your class, you do not need to recompile classes that use it. For example, you removed or renamed a public or protected method from this class

public class Logger implements Constants {
   public Logger getLogger(String name) {
         return LogManager.getLogger(name);
   }
}

from your log-1.jar library and released a new version as log-2.jar. When users of your log-1.jar download the new version it will break their apps when they will try to use the missing getLogger(String name) method.

And if you remove Constants interface (Item 17) this will break binary compatibility either, due to the same reason.

But you can remove / rename a private or package private member of this class without breaking the binary compatibility, because external apps cannot (or should not) use it.

Comments