The Main Principles of Object Oriented Programming

Evan Greer
4 min readAug 23, 2020

--

I recently had a technical interview with a company where I was asked what the pillars of object oriented programming were. I did my best to answer it but came up short and did not get the job. I decided it was time to study what the main principles of object oriented programming were. This blog post will discuss what object oriented programming is on a basic level and what the main principles are of object oriented programming.

Object Oriented Programming

Object oriented programming combines variables and functions into units called objects. Objects are instances of a class. A class is the blueprint of an object. A class determines what makes up an object and what an object is capable of doing. Object orient programming considers the different actions that happen in relation to objects. Object oriented programming exists in commonly used languages such as Java, C++, and PHP. The four main principles guiding object oriented programming are encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation

Encapsulation is when an object maintains its state privately within a class. Other objects cannot access the state directly. But, objects can invoke a list of public functions called methods. Communication with the object is done through methods provided, which can change an objects state. This forms a digital protective barrier around the information that separates it from the rest of the code.

A common example of encapsulation is modeling an animal. An animal can be modeled with an Animal class. The class can have private variables such as mood, hunger, and energy and a private method such as an animal sound. The animal object is created from the Animal class. The animal object can call the function to make a sound, but other classes cannot tell the animal to make a sound. The animal class can have several public functions such as feed. The public methods can be called from the outside and can modify the state of the object.

Abstraction

Abstraction is a natural extension of encapsulation. Abstraction is the process of selecting data to show only the relevant details to an object. In object oriented programming, codebases are often extremely large. So, maintaining a large codebase is challenging. Applying abstraction means an object only exposes a high level mechanism for using it. Abstracting hides all the internal implementation details and only reveals operations that are relevant. This mechanism should be easy to use and should rarely change.

By abstracting, we are able to apply the same information for different applications of the object. An additional advantage of abstraction is that it helps to isolate the impact of the changes made to code as the changes will only affect the variables shown. An example of abstraction is a cell phone. We use a cell phone to do specific tasks that are made available to us, but we have no idea what is going on inside the phone to allow us to do the tasks. The internal actions of the cell phone are abstracted away.

Inheritance

Objects often are very similar and share common logic. Inheritance is the ability of one object to acquire some/all properties of another object. A child class can be created by deriving all the properties of a parent class. In this way, classes are reusable and repetitive code is eliminated. The main class is known as a superclass and the classes that inherit from it are subclasses. Subclasses can have unique properties of their own. For example, you could have a fruit superclass which has properties that all fruits share. Then, there can be subclasses that represent an apple for example that has properties of its own that are unique. We can then go one step further and define subclasses that represent types of apples.

Polymorphism

Polymorphism means “many forms or shapes” and describes situations in which an action occurs in different forms. Objects of different types can use the same interface. A parent interface is defined to be reused. Each child class implements its own version of these methods. The language takes care of evaluating the right implementation of the common interface regardless of which child is passed in.

An example is calculating the surface area of a figure. The figure interface is defined in the parent class which determines how to calculate the surface area based on the type of figure passed in. In this way, different types of figures can be treated as the same type of object. If a figure is a triangle, the interface calculates the surface area of a triangle. You can define the interface once and pass in the type of figure as an argument.

--

--

Evan Greer

Flatiron School Software Engineering Immersive Graduate, Denver, Colorado