typescript
/

TypeScript Inheritance

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Inheritance

What is Inheritance?

Inheritance allows a class to reuse properties and methods from another class using the extends keyword.

Basic Example

TypeScriptRead-only
1
class Animal {
  move() {
    console.log('Moving');
  }
}

class Dog extends Animal {
  bark() {
    console.log('Bark');
  }
}

Using super()

TypeScriptRead-only
1
class Animal {
  constructor(public name: string) {}
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }
}

Method Overriding

TypeScriptRead-only
1
class Animal {
  speak() {
    console.log('Animal sound');
  }
}

class Dog extends Animal {
  speak() {
    console.log('Bark');
  }
}

Access Modifiers in Inheritance

Protected members can be accessed in subclasses, while private members cannot.

Inheritance Benefits

  • Code reusability
  • Cleaner structure
  • Easy maintenance
  • Supports OOP principles

Best Practices

  • Use inheritance for shared logic
  • Avoid deep inheritance chains
  • Override methods carefully
  • Use protected for shared properties

Common Mistakes

  • Overusing inheritance
  • Not calling super() in constructor
  • Breaking encapsulation
  • Confusing inheritance with composition

Conclusion

Inheritance helps build scalable and maintainable applications by enabling code reuse and hierarchical relationships.

Try it yourself

class A {}
class B extends A {}

Test Your Knowledge

Q1
of 3

Inheritance keyword?

A
extends
B
implements
C
class
D
new
Q2
of 3

super() used for?

A
Child
B
Parent constructor
C
Method
D
Variable
Q3
of 3

Private accessible?

A
Yes
B
No
C
Sometimes
D
Always

Frequently Asked Questions

What is inheritance?

Reusing properties from another class.

Keyword used?

extends.

What is super()?

Calls parent constructor.

Previous

ts access modifiers

Next

ts abstract classes

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.