Today I’ll talk about the four essential concepts of object-oriented programming, in a concise and precise manner. They are Abstraction, Encapsulation, Inheritance and Polymorphism.

It’s hard to find material about OOP that is brief, pragmatic and good at the same time.

So, after some research, I could put together the best parts of several materials in this article. Enjoy!

Abstraction

Abstraction is the process of defining the essential characteristics of an object that differentiate it from any other type of object. But this is done without describing details of the implementation.

Abstraction is something that only exists as an idea.

For example, everyone perceive similarly the idea of a table. It has a surface which is sustained by a number of legs, commonly used to place things.

It can be made of wood or glass, or it can have two or three legs, but these characteristics doesn’t matter, because they aren’t essential.

Also, it’s easy to distinguish a table from a ball.

A good example of abstraction is an Interface in Java. It defines the essential behavior of a type of object, through the signatures of methods, but it doesn’t describe the details of the implementation.

public interface Bicycle {
	void changeCadence(int newValue);
	void changeGear(int newValue);
	void speedUp(int increment);
	void applyBreak(int decrement);
}

Abstraction is the foundation that supports other fundamentals of OOP such as inheritance and polymorphism.

Encapsulation

Encapsulation hides the details of the implementation of an object.

But, wait, doesn’t abstraction do the same? No, it doesn’t. As said by Grady Booch in his book Object-oriented Analysis and Design:

Abstraction and encapsulation are complementary concepts: abstraction focuses upon the observable behavior of an object, whereas encapsulation focuses upon the implementation that gives rise to this behavior.

In other words, abstraction allows you to expect a certain behavior of an object and it ensures that your expectation is met, whereas encapsulation ensures that you don’t need to know about the mechanisms behind an object’s behavior to see it working.

For example, when interacting with an object through its methods, it’s not necessary to know the code behind those methods to use them properly.

Dog myDog = new Dog("Spark", 8);

String myDogName = myDog.getName(); // "Spark"
int myDogAge = myDog.getAge(); // 8

myDog.bark(); // Woof!

The main advantage of encapsulation is code maintenance, because code changes can be made independently of other parts.

Also, encapsulation increases code readability.

Inheritance

Inheritance is a mechanism that allows the creation of a class based on a class that already exists.

This new class, which is know as child class (or subclass), has the same attributes and methods as its parent class (or superclass), but it can also have its own attributes.

The main advantages of inheritance are code reuse and the possibility of using polymorphism.

Polymorphism

Polymorphism occurs when the same behavior is implemented in different ways.

An example of polymorphism is when an Interface is implemented in different ways for different classes.

Other example is when a child class overrides a method inherited from its parent class.

The main advantage of Polymorphism is flexibility. I can have an array containing one hundred of different objects, each one is an instance of a different class, but all of them are child classes of the same parent class, so I can call the same method from each one without knowing which class they were instantiated from.


And this is my brief explanation about the 4 pillars of OOP. Hope you have liked it. Feel free to make any suggestions or corrections in the comments section.

Sources

Books
Booch, Grady. Object-Oriented Analysis and Design with Applications - 2nd ed.

Websites
The Four Pillars of Object Oriented Design
Inheritance (object-oriented programming)
What is polymorphism, what is it for, and how is it used?

Videos
What are the 4 Pillars of OOP?