-
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 Inheritance, super() method and prototypal inheritance
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
03 - Chai aur Javascript/12 - Object Oriented Programming/04_inheritance.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,81 @@ | ||
// Parent Class | ||
class Vehicle{ | ||
|
||
// constructor | ||
constructor(brand, model){ | ||
this.brand = brand | ||
this.model = model | ||
|
||
} | ||
|
||
// Method in parent class | ||
startEngine(){ | ||
console.log("Engine Started"); | ||
} | ||
} | ||
|
||
// Child class | ||
class Car extends Vehicle{ | ||
|
||
constructor(brand, model, doors){ | ||
super(brand, model) // call parent class constructor | ||
this.doors = doors | ||
} | ||
|
||
// Method in child class | ||
honkHorn(){ | ||
console.log("Beep! Beep!"); | ||
} | ||
} | ||
|
||
// Creating an instance of the child class | ||
const myCar = new Car("Toyota", "Fortuner", 4); | ||
|
||
myCar.startEngine() // Output: Engine Started | ||
myCar.honkHorn() // Output: Beep! Beep! | ||
|
||
// data dekhte hai | ||
console.log(`Car Brand Name is ${myCar.brand} and Model Name is ${myCar.model}, Plus it has ${myCar.doors} doors.`) | ||
|
||
|
||
// -------------------------------------------------- | ||
|
||
// Method Overriding | ||
|
||
class Animal{ | ||
speak(){ | ||
console.log("Animal Speaks."); | ||
} | ||
} | ||
|
||
class Dog extends Animal{ | ||
// Overriding the speak method | ||
speak(){ | ||
console.log("Bow! Bow!"); | ||
} | ||
} | ||
|
||
const dog = new Dog(); | ||
|
||
dog.speak(); // Output: Bow! Bow! | ||
|
||
|
||
// ---------------------------------------------------- | ||
// Prototypal Inheritance | ||
|
||
// Parent object | ||
const vehicle = { | ||
startEngine: function(){ | ||
console.log("Engine Started"); | ||
} | ||
} | ||
|
||
// Child object | ||
const car = Object.create(vehicle) | ||
|
||
car.honkHorn = function(){ | ||
console.log("Beep! Beep!"); | ||
} | ||
|
||
car.startEngine() // Output: Engine started | ||
car.honkHorn() // Output: Beep! Beep! |