3 ms·
> This problem is impossible to fix People write working code using inheritance, it's not impossible, just requires extra care. One example: class Countin
by ajuc 6y ago
> This problem is impossible to fix
People write working code using inheritance, it's not impossible, just requires extra care. One example:
class CountingStack() {
def push(element) returns nothing =
count += 1; super.push(element)
override def pop() returns element =
count -= 1; super.pop(element)
}
Another way is to use non-virtual methods in the base class where you don't want people to modify your code. In C++ it's even the default. So you should consciously think when you make a method virtual.
class Stack() {
def nonvirtual private push_internal(element) returns nothing = ...
def push(element) returns nothing = push_internal(element)
def pushAll(elementlist) returns nothing = ... // use push_internal if needed
def pop() returns element = ...
}
Anyway - I agree inheritance is bad, and I dislike OOP in general, just wanted to nitpick.
IMHO the main problem is encapsulation. People expect the code to work one way and don't check their assumptions because of the whole tower of abstractions and hiding what actually happens behind layers. And then instead of writing straightforward code to do what has to be done - you have to tiptoe around all possible implementations :)
- valenterry 6y agoYour first example will still break if the base class changes behavior, so I think my point holds. I agree with the non-virtual methods though - they are pretty what I would call conceptual composition.