Slide 1

Slide 1 text

The Perils of Inheritance Why We Should Prefer Composition

Slide 2

Slide 2 text

Ariel Ortiz Full time faculty member Tecnológico de Monterrey Mexico

Slide 3

Slide 3 text

bit.ly/2LpxtF1 Slides available at:

Slide 4

Slide 4 text

Inheritance is-a 4 base derived A base class is also known as a superclass. A derived class is also known as a subclass.

Slide 5

Slide 5 text

Composition has-a 5 composite component A composite instance is composed by (wraps) a component instance.

Slide 6

Slide 6 text

Examples

Slide 7

Slide 7 text

OO Design Principle Favor object composition over class inheritance 7

Slide 8

Slide 8 text

white-box reuse class inheritance black-box reuse object composition In another time, copy/paste was the primary mechanisms of code reuse.

Slide 9

Slide 9 text

ADVANTAGES OF INHERITANCE 9

Slide 10

Slide 10 text

1. Easy way to reuse code Advantage of Inheritance Inheritance is a concept built into Python. When defining a new class all you have to do is list the base classes to inherit their interface and implementation.

Slide 11

Slide 11 text

2. Allows changing inherited implementation Advantage of Inheritance You can also add new behaviours in the derived classes.

Slide 12

Slide 12 text

DISADVANTAGES OF INHERITANCE 12

Slide 13

Slide 13 text

1. The relationship between a base class and a derived class is statically fixed Disadvantage of Inheritance In a class, once you have established what its bases classes are, you can’t dynamically change them.

Slide 14

Slide 14 text

Disadvantage of Inheritance 2. Inheritance supports weak encapsulation and fragile structures Derived classes have privileged access to a base class’s implementation. This means that encapsulation is weak in inheritance relationships. Because of this, it’s possible that a change to a superclass implementation could break subclasses even if the public interface does not change.

Slide 15

Slide 15 text

3. A derived class inherits everything, even things it doesn’t need or want Disadvantage of Inheritance You wanted a banana but what you got was a gorilla holding the banana and the entire jungle. - Joe Armstrong

Slide 16

Slide 16 text

Disadvantage of Inheritance 4. Changes in a base class interface impact all its derived classes If you change the signature of a base class method the change will ripple to all derived classes.

Slide 17

Slide 17 text

CODE EXAMPLE: LIFO LINKED LIST 17 Github link

Slide 18

Slide 18 text

LinkedList + __init__() + insert(value) + insert_all(iterable) + remove() + clear() + __iter__() + __str__() + __len__() InsertCounter + __init__() + insert(value) + insert_all(iterable) + counter()

Slide 19

Slide 19 text

LinkedList + __init__() + insert(value) + insert_all(iterable) + remove() + clear() + __iter__() + __str__() + __len__() InsertCounter + __init__() + insert(value) + insert_all(iterable) + counter() + remove() + clear() + __iter__() + __str__() + __len__()

Slide 20

Slide 20 text

ADVANTAGES OF COMPOSITION 20

Slide 21

Slide 21 text

1. Implementations are configurable at runtime Advantage of Composition You can attach additional responsibilities to an object dynamically. This is the basis of the Decorator design pattern [GAMMA et al].

Slide 22

Slide 22 text

2. Supports good encapsulation and adaptable structures Advantage of Composition Composite classes that use composition are forced to go through the component class interface. That means that they enforce good encapsulation. That also means that changes in implementation of the component classes are less likely to break classes that use them. As long as the interface remains the same, the composite classes won’t break.

Slide 23

Slide 23 text

3. Interface changes have limited ripple effect Advantage of Composition When the interface of a component class changes, it will break the composite classes that rely on the old version of the interface. However, the damage is contained and generally fairly trivial to correct. Because interfaces are not inherited when using composition, the changes affect only the composite class, but not its clients.

Slide 24

Slide 24 text

4. Composition allows a composite class to have relationships with many component classes Advantage of Composition In this case, the composite class is effectively an implementation of the Façade design pattern [GAMMA et al].

Slide 25

Slide 25 text

DISADVANTAGES OF COMPOSITION 25

Slide 26

Slide 26 text

1. Frequently requires more code than inheritance Disadvantage of Composition If a composite class needs to expose some or all of a component class’s interface, it must recreate it. This can be simplified by using in Python a package such as forwardable .

Slide 27

Slide 27 text

2. Often more difficult to read than inheritance Disadvantage of Composition Inheritance establishes a very straightforward relationship. Composition is often less direct and presents a trail that’s more difficult to follow if you’re not familiar with the code.

Slide 28

Slide 28 text

WHEN TO USE INHERITANCE 28

Slide 29

Slide 29 text

1. When the base and derived classes are in the same module/package and under the control of the same programmers When to use inheritance

Slide 30

Slide 30 text

2. When extending classes specifically designed and documented for extension When to use inheritance

Slide 31

Slide 31 text

THINGS TO NOTE 31

Slide 32

Slide 32 text

1. Inheritance is not wrong Things to note Just because you should favor composition does not mean that inheritance is never appropriate. Inheritance is a better solution in some cases.

Slide 33

Slide 33 text

2. Only use inheritance when a new class really does define a subtype of an existing class Things to note You should only use inheritance if there is an “is-a” relationship between two classes.

Slide 34

Slide 34 text

3. Inheritance and composition are not competitors Things to note Although it is true that in almost all cases two classes will be related by either inheritance or composition (and not both), that does not mean that these two types of relationships can not work together. In fact, most classes that use inheritance also use composition.

Slide 35

Slide 35 text

4. Design and document for inheritance Example: JSONEncoder Things to note A class must clearly document any circumstances under which it might invoke an overridable method.

Slide 36

Slide 36 text

5. When using inheritance never forget the Liskov Substitution Principle (LSP) Things to note The LSP is one of the SOLID principles for object oriented design proposed by Robert Martin, a.k.a “Uncle Bob”.

Slide 37

Slide 37 text

The Liskov Substitution Principle: functions that use references to base class objects must be able to use objects of derived classes without knowing it. 37 Formally, LSP is known as the subtype requirement, stated as: Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T.

Slide 38

Slide 38 text

If it looks like a duck, quacks like a duck, but walks on wheels — You most likely have the wrong abstraction. 38

Slide 39

Slide 39 text

CONCLUSION 39

Slide 40

Slide 40 text

SHOULD WE USE INHERITANCE OR COMPOSITION? ¿POR QUÉ NO LOS DOS?

Slide 41

Slide 41 text

Thanks! [email protected] 41

Slide 42

Slide 42 text

Credits Special thanks to: ▪ Presentation template by SlidesCarnival ▪ Icons made by Freepik from Flaticon 42