A Review of OOP and Its Core Principles
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.
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:
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:
If we console.log this alex object, we can see confirm that this is so:
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:
which results in the following:
Principles of OOP
OOP is based on 4 core principles:
- 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.
- 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.
- 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.
- 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 is a very useful coding paradigm. A few of the most popular OOP languages include: