DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Trending

  • MCP Servers: The Technical Debt That Is Coming
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Secure by Design: Modernizing Authentication With Centralized Access and Adaptive Signals
  • Kullback–Leibler Divergence: Theory, Applications, and Implications
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Inherit a Class in TypeScript

How to Inherit a Class in TypeScript

Inheritance is a great way to write less code and get the same functionality. Read on to learn how to use inheritance in your TypeScript code.

By 
Kunal Chowdhury user avatar
Kunal Chowdhury
·
Jul. 16, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
98.7K Views

Join the DZone community and get the full member experience.

Join For Free

Just like any other OOP supported language, TypeScript also allows you to inherit a base class. In the last article, we learned how to create a class in TypeScript. We have also learned how to create a constructor and how to instantiate a class object.

In this article of the TypeScript Tutorial for beginners series, we will learn how to inherit a TypeScript class.


In TypeScript, you can inherit a class from another class. Just use the extends keyword to perform inheritance. Consider the following example to understand it better.

Inheritance in TypeScript

class Person {
 // properties
 firstName: string;
 lastName: string;

 // construtor
 constructor (fName: string, lName: string) {
  // fill the properties
  this.firstName = fName;
  this.lastName = lName;
 }

 // method
 getFullName() {
  return `${firstName} ${lastName}`;
 }
}

class Employee extends Person {
 // properties
 empID: string;
 designation: string;

 // construtor
 constructor (fName: string, lName: string,
     eID: string, desig: string) {
  // call the base class constructor
  super(fName, lName);

  // fill the other properties
  this.empID = eID;
  this.designation = desig;
 }

 // method
 toString() {
  return `${empID} - ${firstName} ${lastName}
    => ${designation}`;
 }
}

Here the Employee class inherits its base Person by writing class Employee extends Person. In the derived class super(...) can be used to call the constructor of the base class. For example, super(fName, lName) in Employee class constructor calls the base class constructor by passing the parameter values fName and lName.

Now, in the following code snippet, we created the instance of the Employee class and passed four parameters to its constructor. In the implementation of the Employee class constructor, we called the base class constructor to pass the first name and last name of the employee. So, when you call the toString() method of the derived class, it will print out both the properties.

let employee: Employee = new Employee("Kunal",
                                      "Chowdhury",
                                      "EMP001022",
                                      "Software Engineer"
                                     );
console.log(employee.toString());

Summary

Let's summarize what we learned today. We learned how to inherit a class from a base class in TypeScript using the extends keyword. Then we discussed how to call a base class constructor by passing the respective values to it. In the next article of this tutorial series, we will discuss interface. Till then, happy learning!

TypeScript

Published at DZone with permission of Kunal Chowdhury, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!