Java 8 introduced the concept of default implementation for interfaces? Isn't this violating Open Closed Principle, since based on the example on https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html, it seems like you can always open up interface to add new features?
Software can be written using the default methods in interfaces and at the same time comply to the open-closed principle or use the default method in interfaces and violate the open-closed principle.
The Open-Closed principle is a design/architecture principle, and it is up to the programmer to follow the principle when designing and programming. The Java language cannot enforce its usage.
Since the Open Closed principle is not enforced by the Java language, it is possible to write a set of classes that violate or adhere to the open-closed principle using JDK 1.0 (the first version of Java) and only the JDK 1.0 set of language features. It is also possible to violate or adhere to the principle when using the interface default methods in interfaces or any of the latest features of Java. Adherence to the Open-Closed principle depends on what is written and how it is written. Java is just a language that you need to express the concepts in.
In Object-Oriented Software Construction (Meyer, Bertrand 1988) The Open-Closed principle is introduced:
"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".
At any point it says that an explicit interface is required (and it's not). Moreover, it clearly says that this also applies to functions, or even whole modules.
Object-oriented code can violate this principle... or not, independently of the syntactical constructions that it employs. It might be true that some particular constructions "invite" OCP violations, but if we are threading this fine, abstract classes would fall in the same bucket.
© 2022 - 2024 — McMap. All rights reserved.