Asserting Types
Learn when and how to assert types of objects and whether their type is part of an inheritance tree.
Introduction
The essence of object-oriented code is summarized as code that has elements of inheritance, encapsulation, and polymorphism. Each of these concepts can be summarized as follows:
- Inheritance is an abstraction for sharing similarities among classes.
- Encapsulation is the grouping some related data items and the methods that operate on that data into a single unit.
- Polymorphism allows access to objects of different types through the same interface.
In the context of inheritance, it is important to distinguish whether an instance is an exact type or whether an instance is an “is-a” type. This distinction is important since it sometimes informs the way tests are written.
Quick recap on inheritance
When speaking about inheritance, we always refer to a base class and a derived class. The derived class is the class that inherits from the base class. The base class is the class being inherited from. In this lesson, we’ll refer to the example of using inheritance to model animals:
Get hands-on with 1400+ tech skills courses.