An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes to extend. Abstract classes can contain both abstract methods (which must be implemented in derived classes) and regular methods (which can be shared among derived classes).
Example: Abstract Classes
// Abstract class with abstract and regular methods
abstract class Animal {
abstract makeSound(): void; // Abstract method - must be implemented in derived classes
move(): void {
console.log("roaming the earth...");
}
}
Key Points:
- Abstract Methods: These methods are declared in the abstract class but do not have an implementation. The derived class must implement these methods.
- Regular Methods: Abstract classes can also have regular methods that provide a default implementation, which can be inherited by derived classes.
Example 2: Abstract Class with a Subclass
abstract class Department {
constructor(public name: string) {}
printName(): void {
console.log("Department name: " + this.name);
}
abstract printMeeting(): void; // Abstract method - must be implemented in derived classes
}
// Subclass extending abstract class
class AccountingDepartment extends Department {
constructor() {
super("Accounting and Auditing"); // Must call `super()` in derived class
}
printMeeting(): void {
console.log("The Accounting Department meets each Monday at 10am.");
}
generateReports(): void {
console.log("Generating accounting reports...");
}
}
// Trying to instantiate an abstract class (This will throw an error)
let department: Department; // Allowed to create a reference to an abstract class
// department = new Department(); // Error: Cannot create an instance of an abstract class
department = new AccountingDepartment(); // Correct: Can create an instance of a subclass
department.printName();
department.printMeeting();
// department.generateReports(); // Error: method doesn't exist on the abstract type
Key Points in the Example:
-
Abstract Class
Department
:- The
Department
class has a regular method (printName
) and an abstract method (printMeeting
). - The
printMeeting
method must be implemented by any class that extendsDepartment
.
- The
-
Subclass
AccountingDepartment
:- The
AccountingDepartment
class implements the abstractprintMeeting
method and adds its own methodgenerateReports
. AccountingDepartment
can be instantiated, whileDepartment
cannot because it is abstract.
- The
-
Abstract Method Implementation:
- Abstract methods like
printMeeting
must be implemented in derived classes. If a derived class doesn’t implement them, it will result in a compilation error.
- Abstract methods like
Example Usage:
- Instantiating: You cannot directly create an instance of an abstract class like
Department
(e.g.,department = new Department();
would cause an error). Instead, you instantiate the derived classAccountingDepartment
. - Calling Methods: Once an instance of a concrete subclass is created, you can call both the abstract and regular methods. For example, calling
department.printName()
works, but callingdepartment.generateReports()
results in an error because the abstract reference type doesn’t includegenerateReports
.