-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about OOP classes and Objects, Encapsulation and Abstraction …
…concepts
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
03 - Chai aur Javascript/12 - Object Oriented Programming/01_basic_class_and_object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Class declaration | ||
class Person{ // class ka naam Person hai | ||
|
||
// constructor method se object initialize hoga | ||
constructor(name, age){ | ||
this.name = name | ||
this.age = age | ||
} | ||
|
||
// Method ye greet method object ke behaviour ko define karega | ||
greet(){ | ||
console.log(`Hello my name is ${this.name} and I am ${this.age} years old`); | ||
} | ||
} | ||
|
||
// Creating an object from the class | ||
const person1 = new Person('Sohail', 21) | ||
person1.greet() // Output: Hello my name is Sohail and I am 21 years old. | ||
|
||
// Another object | ||
const person2 = new Person('Jamil', 20) | ||
person2.greet() |
42 changes: 42 additions & 0 deletions
42
03 - Chai aur Javascript/12 - Object Oriented Programming/02_encapsulation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Encapsulation | ||
|
||
class BankAccount{ | ||
|
||
// #balance ek private field hai jo directly class ke bahar se accessible nahi hai. | ||
#balance | ||
|
||
constructor(initialBalance){ | ||
this.#balance = initialBalance // initialize private field | ||
} | ||
|
||
// Public Methods: deposit, withdraw, aur getBalance public methods hain jo object ke internal state ko access aur modify karne ki permission dete hain. | ||
|
||
// Public method banate hai money deposit karne ke liye | ||
deposit(amount){ | ||
if(amount > 0){ | ||
this.#balance += amount | ||
} | ||
} | ||
|
||
// Public method banate hai money withdraw karne ke liye | ||
withdraw(amount){ | ||
if (amount > 0 && amount <= this.#balance) { | ||
this.#balance -= amount | ||
} | ||
} | ||
|
||
// Public method banate hai balance dekhne ke liye | ||
getBalance(){ | ||
return `Balance is ${this.#balance}` | ||
} | ||
} | ||
|
||
const account1 = new BankAccount(1000); | ||
account1.deposit(1000) // 1000 deposit hua to balance hua 2000 | ||
console.log(account1.getBalance()) // 2000 | ||
|
||
account1.withdraw(1200) // 1200 withdraw karne pe 800 balance hona chahiye | ||
console.log(account1.getBalance()) | ||
// Upar ka methods ke through private property access horahi hai par agar private property ko direct access karne ki koshish karenge to dekhte hai kya hota hai | ||
|
||
// console.log(account1.#balance) // // SyntaxError: Private field '#balance' must be declared in an enclosing class |
23 changes: 23 additions & 0 deletions
23
03 - Chai aur Javascript/12 - Object Oriented Programming/03_abstraction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Abstraction | ||
class Car{ | ||
constructor(make, model){ | ||
this.make = make | ||
this.model = model | ||
} | ||
|
||
|
||
// Abstracted Method | ||
startEngine(){ | ||
console.log("Engine started"); | ||
} | ||
|
||
// Abstracted Method | ||
drive(){ | ||
console.log("Car is Driving."); | ||
} | ||
} | ||
|
||
const myCar = new Car("Toyota", "Fortuner") | ||
|
||
myCar.startEngine() | ||
myCar.drive() |