|13. Classes आणि OOP
Chapter 13JavaScript Tutorial~1 min read

Classes आणि OOP

Object-Oriented Programming in JavaScript

OOP म्हणजे Object-Oriented Programming — real-world entities ला code मध्ये model करण्याची approach. Class म्हणजे objects चा blueprint. एकाच class मधून अनेक objects बनवता येतात.

Marathi Analogy

🏭 Analogy: Car factory चे blueprint (class) एकदा बनवतात. त्यातून अनेक cars (objects) बनतात. प्रत्येक car ला स्वतःचा color, model number असतो (properties), पण सगळे same blueprint follow करतात.

Class Define करणे

Class Syntax

javascript
class Person {
    // Constructor — object बनताना call होतो
    constructor(name, age) {
        this.name = name;   // this = current object
        this.age = age;
    }

    // Method — object चे behavior
    greet() {
        return `नमस्कार! माझे नाव ${this.name} आणि वय ${this.age}.`;
    }

    // Another method
    isAdult() {
        return this.age >= 18;
    }
}

// Objects (instances) बनवणे
let person1 = new Person("Rahul", 25);
let person2 = new Person("Priya", 17);

console.log(person1.greet());    // नमस्कार! माझे नाव Rahul...
console.log(person2.isAdult());  // false

Inheritance — Class extends करणे

Inheritance

javascript
class Animal {
    constructor(name) {
        this.name = name;
    }
    speak() {
        return `${this.name} काहीतरी बोलतो.`;
    }
}

// Dog extends Animal — Animal चे सगळे properties/methods inherit
class Dog extends Animal {
    constructor(name, breed) {
        super(name); // Parent constructor call
        this.breed = breed;
    }
    speak() {
        return `${this.name} भुंकतो: Woof! 🐶`;
    }
}

let dog = new Dog("Tommy", "Labrador");
console.log(dog.speak());  // Tommy भुंकतो: Woof! 🐶
console.log(dog.name);     // Tommy (inherited from Animal)

OOP चे 4 Pillars

  • Encapsulation: Data आणि methods एकत्र ठेवणे. Private data बाहेर expose नाही करणे.
  • Inheritance: Parent class च्या properties/methods child ला मिळतात — code reuse.
  • Polymorphism: Same method name, वेगळे behavior. Dog आणि Cat दोन्हींना speak() method — output वेगळे.
  • Abstraction: Complex implementation लपवणे, simple interface देणे.

Practical Example — Student Class

javascript
class Student {
    constructor(name, marks) {
        this.name = name;
        this.marks = marks;
    }

    getGrade() {
        if (this.marks >= 90) return "A+";
        if (this.marks >= 75) return "A";
        if (this.marks >= 60) return "B";
        return "C";
    }

    toString() {
        return `${this.name}: ${this.marks} marks → Grade ${this.getGrade()}`;
    }
}

let students = [
    new Student("Rahul", 85),
    new Student("Priya", 92),
    new Student("Amit", 67)
];

students.forEach(s => console.log(s.toString()));

Key Points — लक्षात ठेवा

  • Class = objects चा blueprint | Object = class चा instance
  • constructor() — object बनताना automatically call होतो
  • this = current object reference
  • extends = inheritance, super() = parent constructor call
  • 4 Pillars: Encapsulation, Inheritance, Polymorphism, Abstraction
0/13 chapters पूर्ण