In Java when you add a new method to an interface, you break all your clients. When you have an abstract class, you can add a new method and provide a default implementation in it. All the clients will continue to work.
I wonder why the interface is designed this way?
All the old methods are still there, so seems like there is no backward compatibility issue. (Of course there need to be certain exceptions, but I think enabling to add new methods to java interfaces without breaking the clients could be really good idea…)
I’d appreciate your comments.
There are a few possible breaks I can see
IMHO, It’s the subtle problems which are more likely to cause you grief. However, I wouldn’t assume that simply adding a method will break the code and I wouldn’t assume that if a client’s code isn’t getting runtime error means they are using the latest version of anything. 😉
If I compile this code
it prints
I then uncomment method2() and recompile just the interface. This means the interface has a method the
Maindoesn’t implement. Yet when I run it without recompilingMainI getIf I have
and I run with
// method2()commented out, I don’t have a problem.