OOP (Object-Oriented Programming) — Introduction to OOP
Object-Oriented Programming (OOP) is a programming approach that organizes code into objects instead of just functions and logic. An object represents a real-world entity and combines both data (attributes) and behavior (methods). This makes programs easier to understand, reuse, and maintain. OOP helps developers design software that is more structured and modular. Imagine a car: its properties like color, model, and speed are the data, while actions like drive() or brake() are the methods. In OOP, we create classes that act like blueprints for objects. You can then create multiple objects from the same class. For example, a class 'Car' can be used to create many car objects like 'car1' and 'car2'. OOP is widely used in languages like Java, Python, C++, and C#. The four main pillars of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles help in writing clean, reusable, and efficient code. OOP is especially useful for large applications like games, web apps, and desktop software because it makes complex systems easier to manage by breaking them into smaller, logical parts.
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
drive() {
console.log(this.brand + ' is driving');
}
}
let myCar = new Car('Toyota', 'Red');
myCar.drive();