Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
What are ‘implements’ clauses in TypeScript ?
Next article icon

What are Abstract Classes in TypeScript?

Last Updated : 23 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An abstract class in TypeScript serves as a blueprint for other classes and cannot be instantiated directly.

  • It may include abstract methods without implementation, which must be defined in any subclass.
  • Additionally, it can contain concrete methods with implementations, properties, and other members.
JavaScript
abstract class Animal {   abstract makeSound(): void;    move(): void {     console.log('Moving...');   } }  class Dog extends Animal {   makeSound(): void {     console.log('Bark');   } }  const myDog = new Dog(); myDog.makeSound(); myDog.move(); 
  • The Animal class is declared as abstract, containing an abstract method makeSound() without implementation and a concrete method move() with implementation.
  • The Dog class extends Animal and provides an implementation for the makeSound() method.
  • An instance of Dog is created, and both makeSound() and move() methods are called, demonstrating the use of both abstract and concrete methods.

Output:

Bark
Moving...

Key Features of Abstract Classes

  1. Cannot Be Instantiated: It is impossible to instantiate abstract classes directly and any such attempt will fail at compilation.
  2. Abstract Methods: In this methods declared within the abstract class without any implementation and subclasses must provide specific implementation for them.
  3. Concrete Methods: Abstract classes can have fully implemented methods, which can be inherited and used by subclasses.
  4. Can Include Properties and Constructors: Abstract classes could have constructors as well as properties though they cannot be instantiated, thus, typically used to set up common logic or initialize shared properties for derived classes.
  5. Polymorphism: Polymorphism is provided by abstract classes which enable treating its children like instances of itself.

Applications of Abstract Classes

  1. Defining a Common Interface: Abstract classes define common interface for related classes and in this way there is a shared structure of all subclasses to make it easier to understand and maintain the codebase.
  2. Code Reuse: Common methods and properties that an abstract class contains can be inherited by child objects which reduce duplication of efforts during coding, it also makes your application easier to maintain.
  3. Enforcing Implementation: Subclasses must implement specific implementations through abstract methods in an abstract class, it is when certain methods have different implementations depending on various sub-classes.
  4. Encapsulation of Shared Logic: Changes can be managed centrally by containing them within abstract classes that are responsible for many similar sub-classes.

More Example of Abstract Classes in TypeScript

Abstract Class with Abstract Property

JavaScript
abstract class Person {   abstract name: string;    display(): void {     console.log(this.name);   } }  class Employee extends Person {   name: string;   empCode: number;    constructor(name: string, code: number) {     super();     this.name = name;     this.empCode = code;   } }  const emp = new Employee('James', 100); emp.display(); 
  • The Person abstract class declares an abstract property name and a concrete method display() that logs the name.
  • The Employee class extends Person, providing an implementation for the name property and adding an empCode property.
  • An instance of Employee is created with the name 'James' and code 100, and the display() method is called to output the name.

Output

James

Abstract Class with Abstract Method

JavaScript
abstract class Shape {   abstract getArea(): number;    printArea(): void {     console.log(`The area is ${this.getArea()}.`);   } }  class Circle extends Shape {   radius: number;    constructor(radius: number) {     super();     this.radius = radius;   }    getArea(): number {     return Math.PI * this.radius * this.radius;   } }  const circle = new Circle(5); circle.printArea(); 
  • The Shape abstract class includes an abstract method getArea() and a concrete method printArea() that prints the area.
  • The Circle class extends Shape, implementing the getArea() method to calculate the area of the circle.
  • An instance of Circle is created with a radius of 5, and the printArea() method is called to display the area.

Output:

The area is 78.53981633974483.

Best Practices for Using Abstract Classes in TypeScript:

  • Use Abstract Classes for Shared Functionality: When multiple related classes share common behavior, define an abstract class to encapsulate this shared functionality. This promotes code reuse and consistency.
  • Define Abstract Methods for Mandatory Implementations: If certain methods must be implemented by all subclasses, declare them as abstract in the base class. This enforces a contract for derived classes to provide specific implementations.
  • Avoid Instantiating Abstract Classes: Abstract classes are not meant to be instantiated directly. Ensure that only their subclasses are instantiated to maintain the intended design pattern.

Next Article
What are ‘implements’ clauses in TypeScript ?
author
pankajbind
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript
  • Abstract Class and Interface
  • TypeScript-Questions

Similar Reads

  • Typescript Abstract Class
    TypeScript, a superset of JavaScript, introduces many features to improve code organization, readability, and maintainability. One of these features is abstract classes. This article will explain what abstract classes are, how they work, and how to use them effectively in your TypeScript projects. T
    3 min read
  • What are ‘implements’ clauses in TypeScript ?
    In Typescript an implements clause can be used to verify that a class conforms to a specific interface. If a class fails to implement an interface correctly, an error will be generated. Classes can implement a single interface or multiple interfaces at once. Example 1: An interface A is created with
    2 min read
  • What are Generics in TypeScript ?
    In this article, we will try to understand all the facts as well as the details associated with Generics in TypeScript along with some coding examples. Generics in TypeScript: Whenever any program or code is written or executed, one major thing one always takes care of which is nothing but making re
    3 min read
  • How to Extend abstract class with Generics in Typescript ?
    In Typescript, an abstract class is the base class that is inherited by other classes without having to define its members. Generic is the feature with which you can create a variable that represents a type in classes, functions, and type aliases that don't need to define the types that they use exp
    3 min read
  • What are TypeScript Interfaces?
    TypeScript interfaces define the structure of objects by specifying property types and method signatures, ensuring consistent shapes and enhancing code clarity. Allow for optional and read-only properties for flexibility and immutability.Enable interface inheritance to create reusable and extendable
    4 min read
  • What are the Modules in Typescript ?
    Modules in TypeScript allow you to organize code into reusable, manageable, and logical units by encapsulating functionalities into separate files. They help avoid global namespace pollution by providing scoped declarations.Modules can be imported and exported, enabling code reuse and better maintai
    4 min read
  • What is Type Predicates in Typescript ?
    In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narro
    3 min read
  • What is Declaration Merging in Typescript ?
    In Typescript, the term "declaration merging" refers to the compiler combining two declarations with the same name into a single definition. Both of the initial declarations are present in this combined definition. It is possible to merge Interfaces, namespaces and enums and so on but classes cannot
    3 min read
  • What are Arrow/lambda functions in TypeScript ?
    Arrow functions in TypeScript (also known as lambda functions) are concise and lightweight function expressions using the => syntax. Provide a shorter syntax for defining functions.Automatically bind the context of their surrounding scope.It is commonly used for callbacks, array methods, and simp
    3 min read
  • What are Recursive Types & Interfaces in TypeScript ?
    In TypeScript, recursive types and interfaces are constructs that reference themselves within their definitions, enabling the modeling of complex, nested data structures. They are particularly useful for representing hierarchical data such as trees and linked lists.By allowing types to be defined in
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences