Object-Oriented Programming

A Review of OOP and Its Core Principles

Image for post
Image for post
Photo by Eric Prouzet on Unsplash

What is OOP?

Object-oriented programming, often referred to as just OOP, is a programming pattern that revolves around the concept of objects. Objects are bundles of code that contain attributes and have behaviors in the form of methods. This idea of objects allows us to create applications that more closely mimic the way the real world works. OOP allows us to structure our code in a way that makes it possible to reuse our code and easily share functionality throughout an application.

Classes as Blueprints

In OOP, classes are an important concept. A class can be thought of as a blueprint for creating individual object instances. Much like a blueprint for a house, a class contains the specifications for building out an object. A class lays out the attributes (but does not assign values to the attributes) that an instance of the class will have and defines shared behaviors that members of the class have. Importantly, classes contain the instructions for creating new objects and have the ability to create those objects.

An object, then, is a specific instance of a class. It is within an object that values can actually be assigned for the attributes defined in the class.

Src: Get Synapse

To illustrate the difference between classes and objects, we can think of a Cat class. In this Cat class, we can specify the attributes (let’s say name, color, and sex) that all members of the class will have as well as define any shared behaviors, like so:

Image for post
Image for post

Now let’s say we want to create an instance of this Cat class. We can go ahead and create a Cat instance object by writing the below code:

Image for post
Image for post

If we console.log this alex object, we can see confirm that this is so:

Image for post
Image for post

Once this alex object has been instantiated with the passed in values for the name, color, and sex properties, it can access the methods inside of the class by calling on the method:

Image for post
Image for post

which results in the following:

Image for post
Image for post

Principles of OOP

OOP is based on 4 core principles:

  1. Encapsulation: Encapsulation refers to the bundling of data and the methods that operate on that data into one single unit. It is about keeping information private and exposing only what is needed where it is needed. Each object keeps its data private inside of the class it belongs to and can manage its own state by calling on methods available to it.
  2. Abstraction: Abstraction is the idea of hiding unnecessary information from users and only revealing the relevant stuff. It is a way of handling and reducing complexity in OOP.
  3. Inheritance: Much like inheritance in families, inheritance in OOP allows one class to take on the properties of a parent class. This allows us to reuse shared information and put any unique information into a separate class, allowing for shorter, easier to handle code.
  4. Polymorphism: Polymorphism occurs when we have classes related through inheritance. It refers to the way in which a child class can be used similarly to its parent class, but allowing each child class to implement its own version of these methods.

OOP Languages

OOP is a very useful coding paradigm. A few of the most popular OOP languages include:

  • JavaScript
  • Python
  • C++
  • Ruby
  • PHP
  • Java

Fullstack Software Engineer based in NYC. Passionate about creating social change and making a difference through innovative, powerful technologies.

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