My understanding of object-oriented programming

Tomas Gonzalez
6 min readDec 5, 2020

--

Photo by Emile Perron on Unsplash

Edition 1 of ???

My goal for this article is to display my knowledge and understanding of the object-oriented paradigm in programming.

The basic meaning of a programming paradigm is how a programmer writes and organizes their code.

Object-Oriented Programming

In object-oriented programming, an engineer organizes data and logic in objects. Or, the engineer stores the data and logic in classes and then in objects. Some languages that support object-oriented programming are C#, Java, Ruby, Python and JavaScript.

The four main concepts of object-oriented programming are: encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation

In object-oriented programming, developers combine a group of related variables and functions into an object. This is called, encapsulation.

Variables in OOP are referred to as properties, and functions are referred to as methods. It’s important to note that properties define some value, and method or functions define some logic.

In lament terms, all things in a project can be represented as real-life objects (such as a car, a hotel, or a shopping cart).

If you think about a hotel as an object, it might have different properties, such as: name, rating, available rooms, and bookings. Its methods might be something like: makeBooking(), cancelBooking(), and checkAvailability().

Abstraction

A simple idea of abstraction could be that of a car. When the general person goes to see a car for the first time, the only thing they notice is the exterior of the vehicle. The person doesn’t really recognize the motor that powers the vehicle or what is happening under the hood. They experience the vehicle, really, by powering it on and taking it for a drive. Sure, they may understand how to drive the car, but someone else (an engineer, maybe) took care of creating the vehicle to begin with.

One could say the driver is abstracted from the details underneath the hood that powers the vehicle. In other words, the driver doesn’t care much about how the vehicle is powered, but what the driver does care about is that the car moves if they step on the gas pedal.

Same, in OOP, objects can interact with one another without really caring about how each object processes its own instructions. Rather the objects in OOP, if they are communicating with one another, only care about the input and output of each object.

This concept allows two things to happen: a simpler interface to interact with and a reduction to the impact of change.

A developer can change the properties or methods inside of an object, and as long as the input and output remain the same, the rest of the application remains intact.

Inheritance

In programming, it is HIGHLY encouraged to write DRY code. The acronym “DRY” in code means Don’t Repeat Yourself. Inheritance in OOP is important when thinking about DRY code.

Most of the time in programming, objects need similar properties and functions as other objects in an application. If a programmer wrote both objects separately, they would write the same code for the same properties and functions multiple times.

Rather than rewrite code numerous times, a developer can create a parent object and a child object can inherit the parents properties and functions. That same child can have multiple children after that, and each child inherits the properties from its parent object.

Let’s revisit an instance of a vehicle object. That vehicle object could have some properties such as seats, wheels, and doors. With inheritance, a car object and a truck object can grab the properties from the vehicle object.

A car object might want to include a property of trunk, and a truck object may want to include a property of a bed. From there, there might be different types of cars(such as a sports car, or station wagon) and trucks(such as a large or small truck) that stem from those types.

In programming terms, inheritance allows an object to inherit properties and methods from an existing object.

Polymorphism

Let’s say our car object in our inheritance section has a method of honk. A car and truck object can both inherit the honk method, but when called upon will have a different output from one another. A car might have more of a squeaky type sound and a truck might have more a blow-horn type sound.

In object orientation, the same method can be implemented in each object, but the method will behave differently depending on the type of object.

Pros and cons to object-oriented programming

Pros: Object-oriented programming makes it easy to understand the basic concept of objects and also to interpret the meaning of method calls. This paradigm also tends to use an imperative style rather than a declarative style, which reads like a straight-forward set of instructions for the computer to follow.

OOP reduces complexity, increases reusability, isolates impact of change, eliminates redundancy, and allows simplicity of refactoring code.

Cons: Object-oriented programming typically depends on shared state. Objects and behaviors are usually tacked together on the same entity, which may be accessed at random by any number of functions with non-deterministic order, which may lead to undesirable behavior such as race conditions.

Defining Objects in JavaScript

Object literal: In JavaScript, object literals are defined by a variable that is equal to a list of key-value pairs wrapped in curly braces. The object encapsulates data into itself and keeps its variables away from other objects.

Here’s an example of an object with object literal syntax:

let person = {
name: "Tomas", <--- Remember, properties store values
age: 28, <--- Remember, properties store values
sayHello: function(){
console.log("hello") <--- Remember, functions are methods
}
}
person.sayHello(); <--- calling the object and it's method

Here’s a con for object literal syntax in JavaScript. If an object literal has a method value (also known as behavior) within itself it cannot be duplicated. The reason for this is because if the object is duplicated and the method needs to be changed, the programmer would need to go back and change that method in multiple places.

A solution to creating an object with behavior that needs to be duplicated is to use a factory or constructor function instead.

Factory Function: In Javascript, when a function returns an object, is not a class or constructor, and doesn’t need the new keyword, it’s called a factory function.

Here’s an example using our person object:

function createPerson(name) {
return {
name, <--- in ES6, if the key and value are equal we can remove
the value of name
age: 28, <--- Remember, properties store values
sayHello: function(){
console.log("hello") <--- Remember, functions are methods
}
};
};
const person = createPerson("Tomas");
console.log(person.name);

Constructor Function: First off, the naming convention of a constructor function is a little different. The first letter that defines the function is always uppercase. Also, the this and new keywords are really what set this type of function apart from the other two.

The first step to creating a constructor function is to define it as a new function, like so:

function Person() {/* instead of returning an object in this function, we utilize the this keyword to define properties within the function*/
}
const person1 = new Person(); //<-- the new keyword here creates a new person object with the properties defined inside of the function.

Without confusing the reader too much, I want to describe the this and new keywords in the constructor function.

The this keyword on its own refers to the window object. But when we call the new keyword, three things happen. When the programmer uses the new keyword, the program creates an empty object. Then when calling the function with the new keyword, this refers to the function as an object. Lastly, it returns the object from the function.

Here’s the final example of a constructor function using our person example:

function Person(name) {   this.name = name;   this.age = 28;   this.sayHello = function(){   console.log("hello")  }};const person = new Person("Tomas");console.log(person.name, person.age);person.sayHello();

Review

For the first edition of this series I went over the four main concepts of object-oriented programming, or OOP. The four concepts are encapsulation, abstraction, inheritance, and polymorphism.

Additionally, I explained three types of ways programmers create/define objects in JavaScript. We use object literals, factory functions, and/or constructor functions to create objects.

--

--

Tomas Gonzalez
Tomas Gonzalez

Written by Tomas Gonzalez

Experienced Ruby on Rails and JavaScript based programmer with a background in journalism, creative content, and multimedia-marketing. I love coffee and food!

No responses yet