Open sajagkarki opened 2 years ago
Example Here's an example of inheritance in JavaScript using classes:
Scenario:
Let's create a basic representation of animals with a name property and a makeSound() method. We can then create subclasses for specific animals with their own unique sounds.
JavaScript class Animal { constructor(name) { this.name = name; }
makeSound() { console.log("Generic animal sound"); } }
class Dog extends Animal { constructor(name, breed) { super(name); // Call the parent constructor with the name this.breed = breed; }
makeSound() {
console.log(${this.name} barks!
);
}
}
class Cat extends Animal { constructor(name, furColor) { super(name); // Call the parent constructor with the name this.furColor = furColor; }
makeSound() {
console.log(${this.name} meows!
);
}
}
const animal = new Animal("Generic"); animal.makeSound(); // Output: Generic animal sound
const dog = new Dog("Fido", "Labrador"); dog.makeSound(); // Output: Fido barks!
const cat = new Cat("Whiskers", "Ginger"); cat.makeSound(); // Output: Whiskers meows! Use code with caution. content_copy Explanation:
Base Class (Animal): Defines the name property and a generic makeSound() method. Subclasses (Dog, Cat): Extend Animal, inheriting its properties and methods. Each subclass adds its own breed or furColor property and overrides the makeSound() method to provide the specific animal sound. Object Creation: We create instances of Animal, Dog, and Cat, demonstrating how each object inherits from Animal and can call its inherited methods. Subclass methods take precedence when called on a subclass object. Practice Platforms:
There are many ways to practice inheritance in JavaScript:
Online Coding Platforms: Websites like https://www.codecademy.com/catalog, https://www.freecodecamp.org/, and https://www.hackerrank.com/ offer interactive coding challenges and exercises where you can experiment with inheritance concepts. Personal Projects: Create small projects that involve different types of objects with shared and unique characteristics. For example, build a simple inventory system with base product and subclass (electronic, clothing) classes. JavaScript Sandbox Environments: Online tools like https://jsfiddle.net/ and https://codepen.io/index.html allow you to write and test JavaScript code snippets without setting up a full project environment. They're great for quick experimentation. Books and Tutorials: Explore resources like "Eloquent JavaScript" by Marijn Haverbeke or online tutorials on inheritance to deepen your understanding with more complex examples and explanations. By practicing inheritance in these ways, you'll gain confidence in using it to structure your JavaScript code effectively.
Sources info
Inheritance is reducing code by creating base class and the other classes inherit the base class. For example //Base class by default is internal, change it to public abstract as best practice. public abstract class BaseDriver // Abstract class so that no one can create the object of base class { public WebDriver driver;
Inherited example //BaseDriver is inhereted by login class public class login: BaseDriver {