Day 13 of 30 of JavaScript
Shoyab khan
Posted on August 30, 2024
Introduction
Hey reader! 👋 I hope you’re doing well. 😊 In the last post, we completed our discussion on arrays, their properties, and methods. Today, we’re diving into Object-Oriented Programming (OOP) in JavaScript, starting from the basics and progressing to more advanced concepts. Let’s get started! 🔥
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to define complex structures in a simpler way.
Example: Imagine you have a car. This car has various properties such as its name, type (sports, luxury, passenger, etc.), number of seats, engine capacity, and more. Instead of managing these properties individually, OOP allows us to encapsulate them within a single entity—an object. We can define a Car class that contains all the properties and methods related to a car, and then create car objects from this class.
class Car {
constructor(name, type, seats, engineCapacity) {
this.name = name;
this.type = type;
this.seats = seats;
this.engineCapacity = engineCapacity;
}
getInfo() {
return `${this.name} is a ${this.type} car with ${this.seats} seats and an engine capacity of ${this.engineCapacity}L.`;
}
}
const myCar = new Car('Audi', 'Luxury', 5, 3.0);
console.log(myCar.getInfo());
In this example, we define a Car
class with properties and methods, and we access them using an object.
Fundamental Blocks of Object-Oriented Programming
Classes:
Classes are blueprints that hold related properties and methods in one place. In our example, theCar
class contains all the relevant details about a car.Objects:
An object is an instance of a class. It allows us to access the properties and methods defined in that class. Note that a class does not occupy memory until an object is created from it.Properties or Attributes:
These are characteristics of the class. In ourCar
class, properties likename
andtype
define the characteristics of the car.Methods:
Methods are actions that objects can perform. In theCar
class, methods might include actions likedrive
,stop
, orhonk
. In our example, we have thegetInfo
method.
Properties of Object-Oriented Programming
Encapsulation:
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some components, similar to how a capsule protects its contents.Abstraction:
Abstraction involves hiding complex implementation details and exposing only the necessary features. For instance, when you apply the brakes in a vehicle, you don’t need to know the intricate mechanics behind it; you just know that applying the brakes slows down the vehicle.Inheritance:
Inheritance allows one class to inherit properties and methods from another class. The class being inherited from is called the parent class (or superclass), while the class that inherits is called the child class (or subclass). For example, aCar
class can inherit from aVehicle
class.Polymorphism:
Polymorphism refers to the ability of different classes to provide a unique implementation of methods that share the same name. For example, aShape
superclass might have acalculateArea()
method, which can be implemented differently in subclasses likeSquare
andCircle
.
Conclusion
That’s it for today’s introduction to Object-Oriented Programming in JavaScript! I hope you found this information helpful. In the next blog, we’ll delve deeper into OOP concepts. Until then, stay connected, and don’t forget to follow me!
Posted on August 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.