Skip to content

Commit

Permalink
Learned about Inheritance, super() method and prototypal inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
sohail019 committed Jul 27, 2024
1 parent 6d9264b commit c5d25bf
Showing 1 changed file with 81 additions and 0 deletions.
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!

0 comments on commit c5d25bf

Please sign in to comment.