4.3. Encapsulation, inheritance, and polymorphism

The technique of putting data and methods inside a single object is called encapsulation. The two other words most commonly used when talking about OO are inheritance and polymorphism.

The idea behind inheritance is that if you already have a class that’s almost like something you need, its a good idea to define a new class that is a child class of the existing class, recoding or overriding some of the new class methods so that they behave differently from the old class. We can draw a picture of inheritance as in Figure 4.4.

Figure 4.4. A class and a subclass

When an inherited method calls the ...

Get Software Engineering and Computer Games 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.