Defining Encapsulation, Inheritance, and Polymorphism
Object Oriented design in C++ with Kristoffer Hebert
What are encapsulation, inheritance, and polymorphism?
A Class in Object Oriented design is a container for data and methods. Classes use encapsulation to manage access to attributes, such as methods and data, contained within itself. In the C++ language, encapsulation in Classes is achieved with attributes like: public, private, protected and static. Public Class attributes are accessible everywhere. Private Class attributes are only accessible to Class methods within itself. Protected attributes are accessible to itself and Child Classes of itself. Static attributes are only available to the Parent Class, but not Class constructed object.
Inheritance in Object Oriented design describes the relationship between Parent Classes when they extend Child Classes. Extend means one class overrides attributes of, or inherits attributes, from another class. In the provided code example you can see that Child Class inherits every attribute of the Parent Class. The Child class overrides the firstName value in the constructor call. This is how you get the names, Tom Sawyer and Suzy Sawyer.
Class inheritance code example
Polymorphism describes the one to many class relationship in Object Oriented design. A parent Class can have multiple child Classes that inherit and override properties. The Transportation Class has two Classes that extend it, Plane and SailBoat, overriding it’s protected attributes and showDetails method.
Keys to remember
Classes are containers that have attributes, called data and method. Classes use encapsulation to manage access to these attributes. In C++, Class Attributes can be managed via private, static, public, protected Class attribute modifiers. Classes inherit the attributes of the Classes they extend. Additionally, Classes can override attributes of the Classes they extend. Parent Classes can have multiple child Classes.