My understanding of object-oriented programming
By: Tomas Gonzalez
Tags: JavaScript, Object Oriented, Internet, Web Development, Software Development

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.