LibraryAbstract classes and abstract methods

Abstract classes and abstract methods

Learn about Abstract classes and abstract methods as part of TypeScript Full-Stack Development

Abstract Classes and Abstract Methods in TypeScript

Abstract classes and methods are powerful tools in object-oriented programming that enforce a contract for derived classes. They are particularly useful in TypeScript for establishing a common structure and behavior that concrete classes must implement.

What is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes. Abstract classes can contain both abstract methods (methods without an implementation) and concrete methods (methods with a full implementation). The primary purpose of an abstract class is to define a common interface and potentially some shared functionality for its subclasses.

Abstract classes cannot be instantiated.

Think of an abstract class like a partially finished product. You can't use it as is, but it provides a foundation for creating complete, usable products (concrete classes).

In TypeScript, you declare an abstract class using the abstract keyword. For example, abstract class Vehicle { ... }. If you try to create an instance of Vehicle (e.g., new Vehicle()), TypeScript will throw a compile-time error. This prevents incomplete or improperly defined objects from being created.

What are Abstract Methods?

Abstract methods are methods declared within an abstract class that do not have an implementation. They are defined using the

code
abstract
keyword followed by the method signature, but without a method body. Subclasses that inherit from the abstract class are required to provide an implementation for all abstract methods.

Abstract methods must be implemented by subclasses.

An abstract method is like a placeholder for a specific action. The abstract class defines that the action must exist, but the subclass decides how that action is performed.

Consider an abstract class Shape with an abstract method getArea(): number;. Any class that extends Shape, such as Circle or Rectangle, must provide its own implementation for getArea(). This ensures that all shapes derived from Shape have a defined way to calculate their area, even if the calculation logic differs.

Key Characteristics and Benefits

FeatureAbstract ClassConcrete Class
InstantiationCannot be instantiated directlyCan be instantiated directly
MethodsCan contain abstract and concrete methodsContains only concrete methods
InheritanceMust be extended by another classCan be extended or used directly
PurposeBlueprint for subclasses, enforces structureRepresents a concrete entity with full functionality

Abstract classes promote code reuse and enforce a consistent structure across related classes, leading to more maintainable and predictable codebases.

Example Scenario

Let's imagine we're building a system for different types of employees. We can create an abstract

code
Employee
class that defines common properties like
code
name
and
code
id
, and an abstract method
code
calculateSalary()
that each employee type must implement differently.

Consider an abstract Employee class. It has a concrete property name and an abstract method calculateSalary(). A Manager class extends Employee and provides its own calculateSalary() implementation, perhaps including a bonus. A Developer class also extends Employee and implements calculateSalary() based on hours worked and an hourly rate. This structure ensures all employee types have a salary calculation, but the specifics are handled by each concrete class.

📚

Text-based content

Library pages focus on text content

What is the main difference between an abstract class and a concrete class?

An abstract class cannot be instantiated directly, while a concrete class can.

What is the obligation of a class that inherits from an abstract class containing abstract methods?

The inheriting class must provide an implementation for all abstract methods.

Learning Resources

TypeScript Abstract Classes - MDN Web Docs(documentation)

Comprehensive documentation on abstract classes and methods in JavaScript, with relevant TypeScript examples.

TypeScript Official Documentation - Classes(documentation)

The official TypeScript handbook covers classes in detail, including abstract classes and methods.

Understanding Abstract Classes in TypeScript - LogRocket Blog(blog)

A practical blog post explaining the concept of abstract classes and their use cases in TypeScript.

TypeScript Abstract Classes and Methods Explained(blog)

An article from freeCodeCamp that breaks down abstract classes and methods with clear examples.

TypeScript OOP: Abstract Classes - YouTube Tutorial(video)

A video tutorial demonstrating the implementation and benefits of abstract classes in TypeScript.

Abstract Classes vs Interfaces in TypeScript - Dev.to(blog)

This article helps differentiate between abstract classes and interfaces, a common point of confusion.

TypeScript OOP Tutorial - Classes, Inheritance, Abstract Classes(video)

A broader OOP tutorial in TypeScript that includes a section on abstract classes.

TypeScript Abstract Class Example - Stack Overflow(documentation)

Community discussions and code examples related to abstract classes in TypeScript.

Object-Oriented Programming in TypeScript - Smashing Magazine(blog)

An in-depth look at OOP principles in TypeScript, including abstract classes.

TypeScript Handbook - Inheritance(documentation)

Specific section from the TypeScript handbook detailing inheritance, which is fundamental to understanding abstract classes.