Java – binary compatibility of abstract classes and subclasses

In Java, I define an abstract class, which contains concrete and abstract methods, and must be independently created by third-party developers Just to make sure: can I make any changes to the abstract class that are compatible with the source class but not binary? In other words, after they compile their subclasses, I can change the abstract class - except for example, adding an abstract method to it or calling the protected method from the subclass, which of course is source incompatible - this way may force them to recompile their subclasses.

Solution

If it's not too late to change your system, I suggest you do so Overlay is usually not a good way to customize functionality because it is very fragile For example, if you later use the method names used by the client (they are now inadvertently overwritten automatically), overwriting may completely destroy the invariants of your class Generally, the better way to provide customization is to provide your client with an interface limited to custom behavior, and then you have a completely specific class that depends on the instance of this interface and delegate to the interface appropriately when necessary Use custom behavior In this way, your code is completely separate from the client code, and they do not interfere with each other

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>