- Introduction to Object-Oriented programming
- Setting up Environment
- Object Literals
- Deep dive into Object Literals
- Factory Functions
- Constructor Functions
- Implementing Abstraction
- Introduction to ES6 / ECMA 2015
- How to do Object-Oriented Programming with ES6 / ECMA 2015
In all the ES5 tutorials, I’ll be using
varto declare variables just to align with the ES5 syntax. But you should never use
constinstead. Read this tutorial to know why?
How are we going to Learn?
That is the reason I’ve decided to write a tutorial series on this topic. I’ll follow KISS format – Keep it simple, stupid. We’ll learn by writing code, doing things. You won’t have to read tons of technical information. After all, it is not your Ph.D. subject, I believe.
A good writer empowers the reader. A poor writer makes them feel stupid.Unknown
What is Object Oriented programming?
It would be more helpful to you if you understand what was the common code design approach before OOP and how it was problematic.
Earlier programs were written with a different approach called Procedural Programming. If you are familiar with programming you must have used this approach knowingly or unknowingly. In a procedural way, you write your variables (or data) and functions separately.
var make = 'Volkswagen;
var model = 'Golf';
var cruisingSpeed = 120;
console.log('Running with speed ' + cruisingSpeed);
Here is a problem…
What if you also have another car Polo and you want to run it with different speed in cruise control? Or maybe you are a car dealer and you have tons of cars of different makes and models, each with different cruising speed? To handle such a scenario you would need to create separate variables for all of them. And to make it more complicated what if some of the cars do not have Cruise Control feature? The logic of running would be different for that car. So you need different functions like runWithCruiseControl, runWithoutCruiseControl etc.
The more code is written with the procedural approach the more it becomes difficult to maintain it. The procedural way evolves around functions, functions, and functions. All dependent on each other that results in the infamous Spaghetti Code. You make a change in one function and it impacts a dozen others.
Here comes the solution – Object Oriented Programming
Did you notice the related things in the above example? Both, car attributes (make, model, speed) and the behavior (function to run the car) are dependent on each other. In other words, data and the business logic to work on that data can be considered as a single unit. In OOP this unit is called an object. We can create as many similar car objects with different data and each car object can call the functions to run, start, stop, etc. In OOP, an object resembles a real-world object like a real car.
Object-oriented programming is nothing but a different and effective way to organize code. It proposes to organise code around objects that can resemble real-world objects. Each object can have some attributes(data) and methods(function) to act on that data. Think about a car. There can be different kinds of cars but each car will have some common attributes like color, make, model but different value for those data. Each car object will have similar abilities to start, stop and run, etc.
You can write structured and highly maintainable code by following Object Oriented Programming principles.
What are the principles of Object Oriented Programming?
Think again about the car example discussed above. There can be 1000s number of cars but each car will have some common attributes like color, make, model but different values. Each car object will have similar abilities to start, stop and run, etc. Both, car’s attributes ( e.g. make, model and speed) and the abilities (function to run the car) are dependent on each other. So we can group together all the related information about a car in a single unit. That describes encapsulation.
Encapsulation is explained with its implementation details in This Tutorial.
Have you ever wondered when you press your car’s accelerator what exactly happens inside? What principles of mechanical engineering applies there? Most of us do not care about it. All we care about is when we press the pedal the car should accelerate. That describes abstraction.
In abstraction, we hide the complexity of a system from the end user and present them with details that matter to them. We hide implementation details and present a simple interface to deal with.
Abstraction is implemented and explained in detail in This Tutorial.
Do you share some (but not all) attributes of your father, grandfather, and great-grandfather? That is due to inheritance. You (child) inherited some of the attributes of your father (parent).
Inheritance is a mechanism in which one object acquires the property of another object. They share a parent-child relationship.
Let’s revisit our car example.
All cars are vehicles. They inherit the attributes of a vehicle like a start, stop, run. Similarly, all bikes are vehicles. They also inherit the same attributes of the vehicle. But cars carry some of their own attributes like emission, no of tires, etc. Similarly, bikes have some of their own attributes like a handle, paddle, gear, etc.
So the vehicle is the parent object. Cars and Bikes are their children. That is an inheritance.
Inheritance is implemented and explained in detail in This Tutorial.
The word polymorphism came from greek roots – poly(many) and morphe(form). It is the ability of a single interface to have multiple forms. Confused???
Let’s revisit our car example.
Tesla, Ford, Toyota all are cars. All car objects will have run() function. But the science (logic) to run the car is different for all such brands. There is a single function run() but it’ll act differently depending on what type of car it is.
Polymorphism is implemented and explained in detail in This Tutorial.
To solve any of the real-world problems you would have to write tons of code. And this is a fast-changing world. The requirements change or your cool app becomes more popular and people start demanding new features. You must know how to write a structured and maintainable code. OOP is the way to do that. And last but not least, even if the interviewer asks you to write code to calculate the average, she checks if you can organize your code around objects.
Quite obvious, I do not expect any prior knowledge of Object-oriented programming. I’ll explain everything about this topic in this tutorial.
- What Is Object-Oriented Programming (OOP)?
- What are the OOP principles?
- Prerequisites for this tutorial
But you told this is not going to be a theoretical class…
I do remember, buddy. Let’s start setting up the development environment in the Next Tutorial, so that we can learn by doing.