Inheritance vs. Subtyping (webinar #24)
HTML-код
- Опубликовано: 26 сен 2024
- We discussed subtyping in OOP and implementation inheritance and compared how they are different and which one is good, which one is evil. The discussion was based on this article: www.yegor256.co...
Great video!!
Wanted to create a small summary about this. Pls correct me if i'm wrong:
Inheritance (implementation inheritance): is a mechanism where one class inherits implementation from another class (since the attributes of this class - superclass - are public or protected). Implementation includes attributes (variables) and the behaviors (methods/functions) that are not abstract, they have a body, they have code.
We have a "class to class" relationship. One class (subclass) has knowledge about the internals of another class (superclass).
Implementation inheritance is all about code reuse. A subclass is a specification of a superclass and nothing guarantees that the contract (the promised behaviors by the superclass) will not be broken. A subclass can easily override the behaviors and do whatever it desires with it.
Advantages:
- Code reuse
Disadvantages:
- Breaks encapsulation because subclasses has knowledge about the internals of it's superclasses
- Maybe a road to complexity. You have the possibility of having multiple ancestors to a class
- Breaks Liskvo's Substitution principle
- Promotes DIP violation
- Promotes coupling between the classes (object's description/template)
Subtyping (interface inheritance): is a mechanism where one class inherits the purpose/meaning of another type (supertype). Meaning the class actually inherits what was promised as behavior (functions/methods) to it's clients, and the implementation is kept at the class itself.
Interface inheritance is all about preservation of purpose/meaning of a type. A subtype is the implementation (the actual code that will execute the behavior promised by it's supertype) of a supertype.
You can have inheritance between the types(interfaces) since you have the scenario below:
1 - One type only inherits the purpose of another type. Meaning no implementation, no variables are inherited. Only the overall meaning of the supertype
2 - To implement a type that inherited another type's purpose, you'll have to implement all the promised behaviors of it's ancestors. So no contract will be broken
Advantages:
- Design by contract: clients of an object relies on the contract (the interface/supertype)'s description, and does not point's directly to the concrete versions of a class
- Promotes Loosely coupled relationship between the objects
- Promotes Liskov's Substitution Principle
- Promotes Object Encapsulation
- Reduces the complexity
- Promotes proper reuse of code through use of objects
Disadvantages:
- At least from my perspective (maybe i'm wrong about this), you may end up with a proliferation of interfaces
Thanks!!
at 49:00 the connection is lost, what did you say about the new java 8 methods?
I guess you can compare implementation inheritance with static methods. Because the parent classes, like your AbstractMetric example basically provide static methods, only with a scope restricted to the derived classes.
inheritance is a type of polymorphism
I want the same on Russian language! ;)
I don't know why some People dislike the Video?
Why do you think?