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:BarkMoving...Key Features of Abstract ClassesCannot Be Instantiated: It is impossible to instantiate abstract classes directly and any such attempt will fail at compilation.Abstract Methods: In this methods declared within the abstract class without any implementation and subclasses must provide specific implementation for them. Concrete Methods: Abstract classes can have fully implemented methods, which can be inherited and used by subclasses.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.Polymorphism: Polymorphism is provided by abstract classes which enable treating its children like instances of itself.Applications of Abstract ClassesDefining 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.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.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.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 TypeScriptAbstract 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.OutputJamesAbstract 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. Comment More infoAdvertise with us Next Article What are Abstract Classes in TypeScript? pankajbind Follow Improve Article Tags : JavaScript Web Technologies TypeScript Abstract Class and Interface TypeScript-Questions +1 More 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.Th 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 maintain 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 simpl 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 Like