Defining Encapsulation, Inheritance, and Polymorphism


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.




I am working toward a simpler, more meaningful world.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kristoffer Hebert

Kristoffer Hebert

I am working toward a simpler, more meaningful world.

More from Medium

Classes in Ruby — simple explanation

Singleton Pattern in Ruby

The Importance Of Debugging / Console.log