JavaScript > Prototypes and Inheritance > Constructor Functions and Classes > super keyword
Using 'super' to Call Parent Class Constructor and Methods
This example demonstrates how to use the super
keyword in JavaScript to call the constructor and methods of a parent class within a child class. Understanding super
is crucial for effective inheritance and code reuse.
Base Class: Animal
This is the parent class, Animal
. It has a constructor that takes a name
argument and a speak
method.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
Derived Class: Dog
This is the child class, Dog
, which inherits from Animal
. The constructor
uses super(name)
to call the Animal
constructor and initialize the name
property. It then adds its own breed
property. The speak
method also uses super.speak()
to call the parent class's speak
method before adding its own behavior.
class Dog extends Animal {
constructor(name, breed) {
super(name); // Call the constructor of the parent class (Animal)
this.breed = breed;
}
speak() {
super.speak(); // Call the speak method of the parent class
console.log(`${this.name} barks!`);
}
}
Creating Instances
This code creates instances of both the Animal
and Dog
classes. When dog.speak()
is called, it first executes the Animal
class's speak
method via super.speak()
, then executes its own code to print 'Buddy barks!'.
const animal = new Animal('Generic Animal');
animal.speak(); // Output: Generic Animal makes a sound.
const dog = new Dog('Buddy', 'Golden Retriever');
dog.speak(); // Output: Generic Animal makes a sound.
// Buddy barks!
Concepts Behind the Snippet
This snippet demonstrates the core concepts of inheritance in JavaScript, specifically using the super
keyword. Inheritance allows you to create new classes based on existing classes, inheriting their properties and methods. super
is used to access and call functions on an object's parent. It is crucial to call super()
within the constructor of a child class before accessing this
. This initializes the this
context with the parent class properties.
Real-Life Use Case
Imagine building a UI library. You might have a base Component
class with common functionality like rendering and event handling. Then, you could create specialized components like Button
, TextInput
, and Dropdown
that inherit from Component
. The super
keyword would allow the specialized components to reuse the core rendering and event handling logic from the base class, while adding their own specific behaviors. For example, the Button
might override the render
method to add specific styling, calling super.render()
first to maintain the basic component structure.
Best Practices
super()
in the constructor of a derived class if you are defining a constructor.super()
is called before accessing this
within the constructor.super.methodName()
to call a method from the parent class when overriding it in the child class.
Interview Tip
Be prepared to explain the purpose of the super
keyword and how it relates to inheritance. Also, understand the difference between calling super()
in the constructor and calling super.methodName()
when overriding a method. Explain why calling super()
is necessary before accessing this
within the constructor of a derived class. Be able to explain scenarios where using super
improves code reusability and maintainability.
When to Use Them
Use inheritance and the super
keyword when you have classes that share common properties and behaviors. It promotes code reuse and reduces redundancy. If you find yourself duplicating code across multiple classes, consider using inheritance to create a base class with the shared functionality.
Memory Footprint
Inheritance generally adds a small overhead to the memory footprint compared to simply duplicating code. Each instance of a derived class will contain a reference to its parent class's prototype. However, the benefits of code reuse and maintainability usually outweigh this slight increase in memory usage, especially in larger applications.
Alternatives
Pros
Cons
FAQ
-
What happens if I don't call
super()
in the constructor of a derived class?
If you don't callsuper()
in the constructor of a derived class that has a constructor, you will get aReferenceError
. You must callsuper()
before accessingthis
in the constructor to properly initialize the instance with the parent class's properties. -
Can I call
super()
multiple times in a constructor?
No, you should only callsuper()
once in the constructor of a derived class. Calling it multiple times can lead to unexpected behavior and potentially corrupt the object's state. -
What if the parent class doesn't have a constructor?
If the parent class does not have a constructor, you don't need to callsuper()
in the derived class's constructor. However, if you want to initialize properties defined in the parent's prototype, it's good practice to include a constructor and callsuper()
.