Does overriding violate the Open/Closed principle?
Asked Answered
H

4

9

The open/closed principle states that a class should be open for extension but closed for modification.

I thought that the modification part referred strictly to altering the source code of the base class. But I had an argument with someone saying that this also involves overriding methods from the base class.

It this interpretation correct?

Huth answered 14/6, 2016 at 11:41 Comment(1)
Method overriding in subtype polymorphism would probably more typyically be associated with Liskov substitution violations (such as in the Circle/Ellipse problem) than anything else.Frisky
W
5

Virtual methods allow replacing behavior of a base class in a derived class, without having to alter the base class and this means you adhere to the Open/Closed principle since you can extend the system without having to modify existing code.

Base classes (that are not purely abstract) however, tend to violate the Dependency Inversion Principle, since the derived class takes a dependency on the base class, which is a concrete component instead of being an abstraction. Remember, the DIP states that:

High-level modules should [...] depend on abstractions.

Besides this, base classes tend to violate the Interface Segregation Principle as well in case they define multiple public (or protected) methods that are not all used by the derived type. This is a violation of the ISP because:

no client should be forced to depend on methods it does not use

Weksler answered 14/6, 2016 at 14:20 Comment(4)
I'd disagree that having concrete methods on a base class violate ISP, as the client isn't forced to depend on them — they don't need to implement methods they won't use (as they're already there), e.g. compared to implementing an interface and then throwing a MethodNotSupportEdexception, or similar. I would say though that the number of unused methods feel correlated to the cohesion between sub and super class: if a lot of them aren't relevant, it's probably a smell that you've extended the wrong class, or another class needs to be extracted for the commonalities.Moussorgsky
@Moussorgsky Having concrete methods on a base class is not a violation of the ISP, but having concrete methods on a base class that aren't used by a derivate actually is an ISP violation. Whether the client uses them or not is irrelevant, since the ISP states "no client should be forced to depend on methods it does not use". The same holds for wide interfaces, where the client just uses 1 out of 10 methods. It still depends on the other 9 methods, that are "already there".Weksler
@Moussorgsky But yes, there is a correlation between the number of unused methods in the base class and how cohesive this class is. Low cohesion is often an indication of a Single Responsibility Violation violation. You'll often see base classes being abused to implement cross-cutting concerns. Having multiple cross-cutting concerns in one base class is a SRP violation.Weksler
to take the argument to its extreme conclusion, that would mean every Java class is in violation of ISP, unless it makes use of all methods exposed by Object itself :) I think I've a different take to yours on what "client" is referring to — I'd see it as the containing class that's operating on the object, rather than the object's own superclass (e.g. if "Garage" contains a "SportsCar" which implements "Car", Garage would be the client here rather than Car), but agree that either way, whether it's a LSP violation or an ISP violation it's not an OCP violation :)Moussorgsky
C
3

"I thought that the modification part referred strictly to altering the source code of the base class."

You thought right.

There is a plethora of ways to make a class extensible and allowing one to inherit from it is one of them. The keyword extend is even used in a few languages to enable inheritance which makes it quite obvious that we aren't modifying, we are extending...

Whether inheritance is the right solution to extensibility or not is another concern, but usually it is not though. Composition should be the preferred way to make classes extensible (e.g. Strategy, Observer, Decorator, Pipes and Filters, etc...)

Centipoise answered 14/6, 2016 at 13:36 Comment(0)
S
2

An override is a lot like a callback that anyone can register. It's like:

if (IsOverridden) CallCallback();
else DefaultImplementation(); //possibly empty

In that sense there is no modification. You are just reconfiguring the object to call the callback instead of doing the default behavior.

It's just like the click event of a button. You wouldn't consider subscribing to an event a modification. It's extension.

Sylph answered 14/6, 2016 at 12:57 Comment(0)
C
0

Form "Adaptive Code via C#" book, virtual methods is a instrument to achieve OCP.

Cumings answered 28/2, 2018 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.