What is OOPS Conecept ?
OOPS (Object-Oriented Programming) is a paradigm based on object .
Yes, you can think of Object-Oriented Programming (OOP) as a set of guidelines or principles for writing code in a structured and organized manner.
Objects contain both functions (or methods) and data. An object provides a public interface to other code that wants to use it but maintains its own private, internal state; other parts of the system don't have to care about what is going on inside the object.
Bilkul bhai, chalo har pillar ki definition ke saath ek example dekhte hain:
Encapsulation (Sangrahan): Encapsulation ek concept hai jismein data aur uske associated methods ko ek single unit ke andar rakha jata hai, jise hum class kehte hain. Ismein data ko private banaya jata hai, jisse direct access nahi ho sake, sirf class ke andar ke methods ke through hi access kiya ja sake.
Example (JavaScript):
javascriptCopy code// Encapsulation example class Car { constructor(make, model) { this._make = make; // Private data this._model = model; } displayInfo() { console.log("Car Make:", this._make); console.log("Car Model:", this._model); } } // Creating an object of Car class let myCar = new Car("Toyota", "Corolla"); // Accessing private data directly (will result in undefined) console.log(myCar._make); // undefined // Accessing private data through public method myCar.displayInfo();
Inheritance (Vanshikaran): Inheritance ek concept hai jismein ek class dusri class ke properties aur methods ko inherit (vansh) kar leti hai. Isse code reusability badhti hai aur hierarchies banate hain.
Example (JavaScript):
javascriptCopy code// Inheritance example class Vehicle { displayInfo() { console.log("This is a vehicle."); } } class Car extends Vehicle { drive() { console.log("Car is being driven."); } } // Creating an object of Car class and calling methods from both classes let myCar = new Car(); myCar.displayInfo(); // Method inherited from Vehicle class myCar.drive();
Polymorphism (Bahuvachanikaran): Polymorphism ek concept hai jismein ek method alag-alag tareeke se behave kar sakta hai, depending on the object it is called upon. Isse flexibility badhti hai.
Example (JavaScript):
javascriptCopy code// Polymorphism example class Shape { draw() { console.log("Drawing shape."); } } class Circle extends Shape { draw() { console.log("Drawing circle."); } } class Square extends Shape { draw() { console.log("Drawing square."); } } // Creating objects of Circle and Square classes and calling draw method let circle = new Circle(); let square = new Square(); circle.draw(); square.draw();
Abstraction (Sankalan): Abstraction ek concept hai jismein sirf zaroori details ko hi dikhaya jaata hai aur unnecessary details ko chhupaya jaata hai. Isse code complexity kam hoti hai aur code ko samajhna aasan hota hai.
Example (JavaScript):
javascriptCopy code// Abstraction example class BankAccount { constructor(balance = 0) { this.balance = balance; } deposit(amount) { this.balance += amount; console.log("Amount deposited. Current balance:", this.balance); } withdraw(amount) { if (amount <= this.balance) { this.balance -= amount; console.log("Amount withdrawn. Current balance:", this.balance); } else { console.log("Insufficient funds."); } } } // Creating an object of BankAccount class and performing transactions let account = new BankAccount(1000); account.deposit(500); account.withdraw(200);