JavaScript
JS08

JS08 Exercises

Practical

  1. Basic Inheritance and Derived Classes

Objective: Create a simple class hierarchy to understand the basic idea of inheritance and the extends keyword.

Create a Base Class: Make a base class named Animal. It should have a constructor that accepts a name parameter and an instance method named speak that logs a generic message.

Create a Derived Class: Create a derived class named Dog that extends Animal. Override the speak method to log a message specific to dogs.

Instantiate and Test: Create an instance of Dog and call its speak method. Check if it displays the dog-specific message.

 
// Base Class
class Animal {
    constructor(name) {
        this.name = name;
    }
 
    speak() {
        console.log(`${this.name} makes a sound.`);
    }
}
 
// Derived Class
class Dog extends Animal {
    speak() {
        console.log(`${this.name} barks.`);
    }
}
 
// Instantiate and Test
const myDog = new Dog("Rex");
myDog.speak();  // Outputs: Rex barks.
 
  1. Using super() and Method Overriding

Objective: Learn to use super() in constructors and method overriding.

Enhance Base Class: Add a new property type to the Animal class. Modify the constructor to accept this as a second parameter.

Modify Derived Class: In the Dog class, use super() in its constructor to pass the name and type to the base class. Override the speak method to include the type in the message.

Instantiate and Test: Create an instance of Dog and test the speak method. Ensure it includes both name and type.

 
// Enhance Base Class
class Animal {
    constructor(name, type) {
        this.name = name;
        this.type = type;
    }
 
    speak() {
        console.log(`${this.name} the ${this.type} makes a sound.`);
    }
}
 
// Modify Derived Class
class Dog extends Animal {
    constructor(name, type) {
        super(name, type);
    }
 
    speak() {
        console.log(`${this.name} the ${this.type} barks.`);
    }
}
 
// Instantiate and Test
const myDog = new Dog("Rex", "dog");
myDog.speak();  // Outputs: Rex the dog barks.
 
  1. Prototype Chain and Object Prototypes

Objective: Explore prototypes, own properties, and the prototype chain.

Working with Prototypes: Create an object using a constructor function (not a class). Investigate its prototype using Object.getPrototypeOf().

Setting Prototypes: Create a new object and set its prototype to the object created above using Object.create().

Prototype Chain: Create a function and add a method to its prototype. Instantiate an object from it and explore the prototype chain, observing how the method is accessible through the chain.

 
// Constructor Function
function Animal(name) {
    this.name = name;
}
 
// Investigating Prototype
let animal = new Animal("Generic Animal");
console.log(Object.getPrototypeOf(animal));
 
// Setting Prototypes
let cat = Object.create(animal);
cat.name = "Whiskers";
 
// Prototype Chain
Animal.prototype.eat = function() {
    console.log(`${this.name} is eating.`);
};
 
cat.eat();  // Outputs: Whiskers is eating.
 
// Exploring Prototype Chain
console.log(cat instanceof Animal);  // true
 
  1. Understanding JavaScript's Single Inheritance Model

Objective: Understand that JavaScript doesn't support multiple inheritance and how this affects class design.

Research Task: Read about JavaScript's single inheritance model and why it doesn't support multiple inheritance.

Conceptual Design: Try to design a class structure for a hypothetical scenario (like a zoo management system) keeping in mind the single inheritance constraint.

Reflection: Write down the challenges and limitations you faced due to the single inheritance model and how you worked around them.