BUGSPOTTER

What is oops concept in Javascript ?

What is oop in Javascript

OOP (Object-Oriented Programming) in JavaScript is a programming paradigm that uses objects and classes to structure code. It focuses on organizing data and behavior into objects that have properties (attributes) and methods (functions).

Concepts of Object-oriented Programming

The basic concepts of Object-Oriented Programming (OOP) are foundational principles that help structure and organize code in a way that is more manageable, reusable, and scalable.

1. Objects

An object in OOP is a self-contained entity that contains both data (attributes or properties) and behaviors (methods or functions). It is an instance of a class and represents a real-world entity. Objects can interact with each other by calling methods, and they can store information through properties.

  • Example:
				
					const car = {
    make: 'Toyota',
    model: 'Corolla',
    year: 2020,
    drive() {
        console.log("Driving the car");
    }
};

Here, car is an object with properties (make, model, year) and a method (drive()).

				
			

2. Classes

A class is a blueprint or template for creating objects. It defines the properties and methods that objects created from the class will have. Classes allow for the creation of multiple objects with the same structure but different data.

  • Example:
				
					class Car {
    constructor(make, model, year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    drive() {
        console.log("Driving the car");
    }
}

const myCar = new Car('Honda', 'Civic', 2022);

In this example, Car is a class with a constructor to
initialize properties and a method drive() to define behaviors.


				
			

3. Encapsulation

Encapsulation is the concept of bundling the data (attributes) and methods (behaviors) that operate on the data into a single unit (object or class). Encapsulation also hides the internal workings of an object from the outside world, exposing only the necessary parts of the object through public methods. This protects the internal state of an object and prevents unintended interference or modification.

  • Example:
				
					class Account {
    constructor(owner, balance) {
        this.owner = owner;
        let _balance = balance;  // private property

        this.getBalance = function() {
            return _balance;  // accessing private balance through public method
        };

        this.deposit = function(amount) {
            if (amount > 0) {
                _balance += amount;
            }
        };

        this.withdraw = function(amount) {
            if (amount > 0 && _balance >= amount) {
                _balance -= amount;
            }
        };
    }
}

const account = new Account("John", 1000);
account.deposit(500);
console.log(account.getBalance());  // 1500

In this example, the balance property is encapsulated within the Account class and 
can only be accessed 
or modified through specific methods like deposit, withdraw, and getBalance


				
			

4. Abstraction

Abstraction is the principle of hiding the complexity of an object and exposing only the necessary functionality. In OOP, abstraction allows us to create a simple interface while hiding the intricate details of the internal workings. This helps in managing complexity and focusing on high-level operations.

  • Example:
				
					class Car {
    constructor(make, model) {
        this.make = make;
        this.model = model;
    }

    startEngine() {
        console.log("Starting the engine...");
    }

    drive() {
        console.log("Driving...");
    }
}

const myCar = new Car('Ford', 'Focus');
myCar.startEngine();  // The user doesn't need to know how the engine starts internally
myCar.drive();  // The user just uses the car

In this example, the user interacts with high-level methods like startEngine() 
and drive(), without needing to understand 
the complex details of how these actions are implemented.


				
			

5. Inheritance

Inheritance is the mechanism that allows one class to inherit properties and methods from another class. It helps in code reuse by allowing the child class to inherit functionality from the parent class, while still enabling the child class to add or modify specific functionality.

  • Example:
				
					class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(this.name + " makes a sound");
    }
}

class Dog extends Animal {
    speak() {
        console.log(this.name + " barks");
    }
}

const dog = new Dog("Buddy");
dog.speak();  // Output: Buddy barks

In this example, 
the Dog class inherits from the Animal class and overrides the speak 
method to provide its own behavior. 
The Dog class inherits the name property 
and can still call the speak method from Anima

				
			

6. Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common parent class. It enables a method to have different behaviors based on the object type. This concept allows for method overriding (where a subclass provides a specific implementation) and method overloading (where methods in the same class may behave differently depending on the input).

  • Example:
				
					class Animal {
    speak() {
        console.log("Animal makes a sound");
    }
}

class Dog extends Animal {
    speak() {
        console.log("Dog barks");
    }
}

class Cat extends Animal {
    speak() {
        console.log("Cat meows");
    }
}

const animals = [new Dog(), new Cat()];
animals.forEach(animal => animal.speak());
// Output:
// Dog barks
// Cat meows
				
			

Latest Posts

  • All Posts
  • Software Testing
  • Uncategorized
Load More

End of Content.

Categories

Enroll Now and get 5% Off On Course Fees