Open Closed Principle Vs Default Implementation
Asked Answered
Y

2

7

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?

Yerkovich answered 18/4, 2017 at 0:30 Comment(3)
This question will most likely be closed since it solicits an opinion. However, given that it is an interesting question, I’ll render mine. The Open Closed Principle is not violated. As long as methods behave as expected per the implicit guarantee provided by the interface “contract” it does not matter if it is implemented as a default interface method, in an associated abstract class or a class implementing the interface. From the consumer’s perspective, nothing has changed.Inexpressible
It's not a case of "opening up the interface". Open-Closed Principle is about an entity that is closed for extension but closed for modification. Default method is a mechanism that allows more freedom in extending the entity, and there is no way you can use a default method to modify existing implementation: even if new added default method is added where implementing class previously had a method with same signature, a default method would never win over the concrete implementation, so nothing about implementation details has changed, just as @Inexpressible said.Photomechanical
As adding a default function to an existing interface does not force developers to make changes in other modules, it does not affect the main objective of the OCP, the code reusability and maintainability and hence does not violates the principle itself. It should also be noted that in his article on OCP Robert Martin also mentioned that It should be clear that no significant program can be 100% closed.Decane
F
0

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.

Fiester answered 11/7, 2020 at 12:2 Comment(0)
B
0

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.

Barefoot answered 24/1, 2021 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.