Sunday, February 22, 2015

Objection Orientation: Polymorphism, Abstraction, Encapsulation, and Inheritance

Developers who are new to object-oriented programming often struggle to understand the long-winded explanations that are scattered across the interwebs.  The intent of this post is to ease the pain (hopefully) by summarizing the main concepts of object orientation in a concise and precise format by providing a definition, two examples, and a brief explanation of its benefits.


Definition:  Polymorphism is the concept where a parent type can have many sub-types.

Example #1:  ArrayList and LinkedList are sub-types of the parent type List.

Example #2:  Car, Motorcyle, and Bus are sub-types of the parent type Vehicle.

Benefits:  Polymorphism allows you to use a single parent type to interact with all possible sub-types.  It also helps to decouple your code.  When you are working with a List object, you can freely swap out the underlying implementation from an ArrayList to a LinkedList and vice-versa.


Definition:  Abstraction is the generalization of common functionality across different objects.

Example #1:  List is an abstraction of ArrayList and LinkedList because it generalizes the common functionality of adding and removing elements.

Example #2:  Vehicle is an abstraction of Car, Motorcyle, and Bus because it generalizes the common functionality of accelerating and braking.

Benefits:  Abstraction allows you to interact with different objects via a common interface.


Definition:  Encapsulation is the packing (hiding) of data and implementation details into a class or function.

Example #1:  Data and implementation details about a car are encapsulated (packed, hidden) into a Car class.  Access to its data can be controlled via public, protected, and private modifiers.

Example #2:  The functionality to accelerate and brake a car is encapsulated (packed, hidden) into functions.  Access to these functions can be controlled via public, protected, and private modifiers.

Benefits:  Encapsulation allows you to hide implementation details and protect against mis-use by other classes.  It also helps to decouple your code.  For example, I could modify the formula in the accelerate function without breaking any code dependencies on that function.


Definition:  Inheritance is the re-use of a base class's functionality by a sub-class.

Example #1:  Car is a sub-class that inherits the functionality of its base class Vehicle, including the data (currentHeading, velocity, weight) and functions (turn, accelerate, brake).

Example #2:  A Car can turn because it inherited the functionality from its base class Vehicle.

Benefits:  Inheritance allows you to re-use functionality and reduce duplicate code.

Polymorphism, abstraction, encapsulation, and inheritance are distinctly different concepts, but together they support the paradigm of object orientation.  I hope this post cleared up any misunderstandings of these four pillars object orientation!