Chapter 8. Elements of object-oriented programming

 

This chapter covers

  • Defining contracts by using interfaces
  • Implementing a hierarchy of expressions
  • Implementing the adapter pattern
  • Extending behavior with mix-ins
  • Considering alternatives to pure OOP

In this chapter, we will cover the elements of object-oriented programming and see how we can employ them effectively. You are probably familiar with these concepts, as they show up in all object-oriented languages, so we’ll focus more on their use cases.

We’ll start with interfaces and see how we can think of them as contracts. After interfaces, we’ll look at inheritance: we can inherit both data and behavior. An alternative to inheritance is composition. We’ll look at some of the differences between the two approaches and when to use which. We’ll talk about extending data and behavior with mix-ins or, in TypeScript, intersection types. Not all languages support mix-ins. Finally, we’ll look at alternatives to OOP and when it might make sense not to use it. This is not because there is something wrong with OOP, but because many developers learn it as the only approach to software engineering, and sometimes it ends up being overused.

Before getting started, let’s quickly define OOP.

8.1. Defining contracts with interfaces

8.2. Inheriting data and behavior

8.3. Composing data and behavior

8.4. Extending data and behavior

8.5. Alternatives to purely object-oriented code

Summary

Answers to exercises

sitemap