Inheritance allows one class to use the properties and methods of another class. It helps us reuse existing code and create relationships between classes.
extends keyword to inherit from
another class. The super() keyword is used to call
the parent class constructor.
Inheritance is an object-oriented programming concept where one class receives properties and methods from another class.
class Animal {
eat(): void {
console.log("Animal is eating");
}
}
class Dog extends Animal {
}
The class whose properties and methods are inherited is called the parent class, base class, or superclass.
class Animal {
eat(): void {
console.log("Eating");
}
}
The class that inherits from another class is called the child class, derived class, or subclass.
class Dog extends Animal {
}
The extends keyword is used to create inheritance
between classes.
class Animal {
}
class Dog extends Animal {
}
class Animal {
eat(): void {
console.log("Animal is eating");
}
}
class Dog extends Animal {
}
const dog = new Dog();
dog.eat();
The Dog class can use the eat()
method inherited from Animal.
class Person {
name: string = "Rahul";
}
class Student extends Person {
}
const student = new Student();
console.log(student.name);
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
class Student extends Person {
}
const student = new Student("Amit");
console.log(student.name);
A child class constructor can call the parent class
constructor using super().
class Person {
constructor(public name: string) {
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
super() calls the constructor of the parent
class.
class Person {
constructor(public name: string) {
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
In a derived class constructor, super() must
be called before accessing this.
class Student extends Person {
constructor(name: string) {
super(name);
console.log(this.name);
}
}
class Person {
constructor(public name: string) {
}
}
class Student extends Person {
course: string;
constructor(name: string, course: string) {
super(name);
this.course = course;
}
}
class Animal {
eat(): void {
console.log("Eating");
}
}
class Dog extends Animal {
bark(): void {
console.log("Barking");
}
}
const dog = new Dog();
dog.eat();
dog.bark();
A child class can have its own methods while also using methods inherited from the parent.
class Vehicle {
start(): void {
console.log("Vehicle started");
}
}
class Car extends Vehicle {
drive(): void {
console.log("Car is driving");
}
}
A child class can provide its own implementation of a method inherited from the parent class.
class Animal {
sound(): void {
console.log("Animal sound");
}
}
class Dog extends Animal {
sound(): void {
console.log("Dog barks");
}
}
const dog = new Dog();
dog.sound();
The child implementation is executed when the method is overridden.
The super keyword can also be used to call a
parent class method.
class Animal {
sound(): void {
console.log("Animal sound");
}
}
class Dog extends Animal {
sound(): void {
super.sound();
console.log("Dog barks");
}
}
A protected member can be accessed inside
the parent class and derived classes.
class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Student extends Person {
showName(): void {
console.log(this.name);
}
}
A private member belongs only to the class
where it is declared and cannot be directly accessed by
a derived class.
class Person {
private age: number = 20;
}
class Student extends Person {
// Cannot directly access age
}
class Person {
public name: string = "Rahul";
}
class Student extends Person {
showName(): void {
console.log(this.name);
}
}
const student = new Student();
console.log(student.name);
A class can inherit from another child class, creating multiple levels of inheritance.
class Animal {
eat(): void {
console.log("Eating");
}
}
class Dog extends Animal {
bark(): void {
console.log("Barking");
}
}
class Puppy extends Dog {
play(): void {
console.log("Playing");
}
}
const puppy = new Puppy();
puppy.eat();
puppy.bark();
puppy.play();
The Puppy object can use methods inherited
through both parent levels.
class Person {
constructor(public name: string) {
}
}
class Student extends Person {
constructor(
name: string,
public course: string
) {
super(name);
}
}
const student = new Student(
"Priya",
"React Native"
);
class Person {
constructor(public name: string) {
}
showName(): void {
console.log(this.name);
}
}
class Student extends Person {
showCourse(): void {
console.log("TypeScript");
}
}
const student = new Student("Amit");
student.showName();
student.showCourse();
class Employee {
constructor(
public name: string,
public salary: number
) {
}
showEmployee(): void {
console.log(this.name);
console.log(this.salary);
}
}
class Manager extends Employee {
constructor(
name: string,
salary: number,
public department: string
) {
super(name, salary);
}
showDepartment(): void {
console.log(this.department);
}
}
const manager = new Manager(
"Rahul",
50000,
"IT"
);
manager.showEmployee();
manager.showDepartment();
class Person {
constructor(public name: string) {
}
showName(): void {
console.log("Name:", this.name);
}
}
class Student extends Person {
constructor(
name: string,
public course: string
) {
super(name);
}
showCourse(): void {
console.log("Course:", this.course);
}
}
const student = new Student(
"Anita",
"React Native"
);
student.showName();
student.showCourse();
extends keyword.super() in a derived constructor.this before calling super().Understanding inheritance is useful when working with TypeScript classes in larger applications. It can help organize shared functionality between related classes.
class BaseService {
log(message: string): void {
console.log(message);
}
}
class UserService extends BaseService {
getUser(): void {
this.log("Getting user");
}
}
const service = new UserService();
service.getUser();
extends creates inheritance between classes.super() calls the parent constructor.super.method() can call a parent method.protected members are useful with inheritance.class Person {
constructor(
public name: string,
public age: number
) {
}
showPerson(): void {
console.log("Name:", this.name);
console.log("Age:", this.age);
}
}
class Student extends Person {
constructor(
name: string,
age: number,
public course: string
) {
super(name, age);
}
showStudent(): void {
console.log("Course:", this.course);
}
}
const student = new Student(
"Rahul",
20,
"React Native"
);
student.showPerson();
student.showStudent();
Here, Student inherits the properties and
showPerson() method from Person.
The student class also adds its own
course property and showStudent()
method.
extends keyword is used for inheritance.super() calls the parent class constructor.super.method() can call a parent class method.Question: Which keyword is used to inherit one class from another class in TypeScript?