Self types versus inheritance

In the previous section, we said that we don't want to use inheritance in order to get access to the Database methods. Why is that? If we had made Persister extend Database, this would mean that it would become a database itself (is-a relationship). However, this is not correct. It uses a database in order to achieve its functionality.

Inheritance exposes a subclass to the implementation details of its parent. This, however, is not always desired. According to the authors of Design Patterns: Elements of Reusable Object-Oriented Software, developers should favor object composition over class inheritance.

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.