0% found this document useful (0 votes)
11 views2 pages

JavaScript OOP Inheritance Explained

The document discusses using inheritance in JavaScript to create base classes and subclasses to represent people in a school system without duplicating code. It defines a base Person class with name and introduceSelf methods, then derives Professor and Student subclasses from Person using the extends keyword. This allows subclasses to inherit common properties from Person while adding their own specific attributes and overriding methods like introduceSelf. Inheritance promotes code reuse, reduced complexity, and polymorphism.

Uploaded by

COD PIRATES
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

JavaScript OOP Inheritance Explained

The document discusses using inheritance in JavaScript to create base classes and subclasses to represent people in a school system without duplicating code. It defines a base Person class with name and introduceSelf methods, then derives Professor and Student subclasses from Person using the extends keyword. This allows subclasses to inherit common properties from Person while adding their own specific attributes and overriding methods like introduceSelf. Inheritance promotes code reuse, reduced complexity, and polymorphism.

Uploaded by

COD PIRATES
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance: Building on Existing Classes in JavaScript (Simplified)

Imagine your school system. You want to represent both Professors and Students in your
program. While they have different roles and responsibilities, they share some common
traits: they both have names and can introduce themselves.

Instead of creating separate classes for each, you can leverage inheritance. This allows
you to reuse code and build on existing classes. Let's see how:

Step 1: Define the "Person" base class:

Think of it as the common ground for both professors and students.

class Person {
constructor(name) {
[Link] = name;
}

introduceSelf() {
[Link](`My name is ${[Link]}.`);
}
}

Step 2: Derive "Professor" and "Student" from "Person":

Use the extends keyword to tell them they inherit from Person.

class Professor extends Person {


constructor(name, teaches) {
super(name); // Call parent constructor
[Link] = teaches;
}

gradePaper(paper) {
[Link](`Grading ${paper}...`);
}

// Override introduceSelf to add professor details


introduceSelf() {
[Link](); // Call parent introduceSelf
[Link](`...and I teach ${[Link]}.`);
}
}
class Student extends Person {
constructor(name, year) {
super(name);
[Link] = year;
}

introduceSelf() {
[Link]();
[Link](`...and I'm in year ${[Link]}.`);
}
}

Benefits of Inheritance:

● Code reuse: Share common features like name and introduceSelf instead of
duplicating code.
● Reduced complexity: Organize your classes logically and avoid repeating yourself.
● Polymorphism: Different classes can implement the same method (e.g.,
introduceSelf) in their own unique way.

Key points to remember:

● Superclass: The "parent" class (e.g., Person) providing the base functionality.
● Subclass: The "child" class (e.g., Professor, Student) inheriting from the superclass
and adding specific details.
● extends: Keyword used to define a subclass based on a superclass.
● super(): Used in the subclass constructor to call the parent constructor and initialize
inherited properties.
● Override: When a subclass redefines a method already present in the superclass.

Example usage:

const walsh = new Professor("Walsh", "Psychology");


[Link](); // "My name is Walsh. ...and I teach
Psychology."

const summers = new Student("Summers", 1);


[Link](); // "My name is Summers. ...and I'm in year
1."

const pratt = new Person("Pratt");


[Link](); // "My name is Pratt."

With inheritance, your code becomes cleaner, more organized, and easier to maintain, just
like a well-structured school system!

Common questions

Powered by AI

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. In the given context, both Professor and Student inherit the introduceSelf method from the Person class. However, both subclasses override this method to include additional information specific to their roles. The Professor class adds the subject taught, while the Student class mentions the year of study. This process allows subclasses to tailor the behavior of inherited methods to their needs while maintaining a consistent interface .

The 'super' keyword is crucial when extending classes in JavaScript as it allows a subclass to call the constructor of its superclass, which initializes the inherited properties. In the example, both Professor and Student classes call 'super(name)' in their constructors to initialize the 'name' property inherited from the Person class. Additionally, 'super' can be used to invoke other methods of the superclass, ensuring that the base functionality is preserved while extending or overriding methods in subclasses .

In the JavaScript code for school system representation, a 'superclass' refers to the Person class, which provides base functionality accessible to derived classes. A 'subclass', such as Professor or Student, inherits properties and methods from the superclass and adds specific functionality. The subclasses use the 'extends' keyword to indicate inheritance from the superclass, and through this hierarchical structure, code duplication is minimized while software design is planned logically and clearly .

Polymorphism is demonstrated in the inheritance of the Professor and Student classes by allowing them to implement the same method, introduceSelf, differently. While both classes inherit this method from the Person superclass, they override it to include specific details; the Professor class adds information about the subject taught, and the Student class adds the current academic year. This allows both classes to have their unique behavior while sharing a common interface .

Overriding the introduceSelf method in both the Professor and Student classes provides a more specialized implementation that caters to the unique information each type of object should convey. While the Person class provides a general introduction, Professor and Student override this method to add additional details pertinent to their roles, such as the subject taught for Professors and the academic year for Students. This enhances the functionality and usability of the method specific to each context .

Inheritance in JavaScript allows subclasses like Professor and Student to derive from a common superclass, in this case, Person. This promotes code reuse by sharing common features, such as the name property and the introduceSelf method, between classes without duplicating code. It reduces complexity by logically organizing the code into a hierarchy where shared functionality is centralized, making the code easier to manage and understand .

Defining a base class, such as Person in the given example, sets a foundation upon which other classes can build. It encapsulates common attributes and methods, such as the name property and introduceSelf method, which can be shared across subclasses. This prevents code redundancy and simplifies maintenance. The base class serves as a template for derived classes like Professor and Student, ensuring consistency and promoting code reuse by having a single, central class to extend .

The Professor and Student subclasses use inheritance to implement their constructors by calling the constructor of the Person superclass with the 'super' keyword, which initializes the shared 'name' property. However, they also introduce specific properties: the Professor class adds a 'teaches' property to specify the subject area, and the Student class adds a 'year' property to indicate the academic year. This integration demonstrates how subclasses can simultaneously utilize inherited functionality and customize their constructors to handle additional information pertinent to their specific roles .

The use of inheritance in organizing a class hierarchy for a school system application offers several advantages, including code reuse, as common methods and attributes are centralized in a superclass, thus reducing redundancy. This also simplifies maintenance and enhances scalability by adding new features or subclasses with minimal changes to existing code. However, potential limitations include increased tight coupling between classes, which can hinder flexibility in certain situations, and potential difficulty in implementing changes that do not fit neatly into the hierarchy, possibly requiring significant restructuring.

The 'extends' keyword establishes an inheritance relationship by indicating that a class is a subclass of another, providing access to inherited properties and methods. In the case of Person and its derived classes Professor and Student, 'extends' allows these subclasses to inherit the functionality defined in Person, such as the name property and introduceSelf method, and extend it with their additional properties and methods. This mechanism facilitates the creation of a more structured and reusable codebase .

You might also like