An abstract class is a special class that is designed to be inherited by other classes. It can contain normal methods, properties, and abstract methods.
An abstract class is a class that provides a common structure for other classes.
abstract class Animal {
}
The abstract keyword is used before the
class keyword.
The abstract keyword is used to define an
abstract class or an abstract method.
abstract class Person {
}
You cannot create an object directly from an abstract class.
abstract class Animal {
}
// Error
// const animal = new Animal();
Abstract classes are useful when several classes share common functionality but also need their own specific implementations.
An abstract method is declared without a method body. The child class must provide its implementation.
abstract class Animal {
abstract makeSound(): void;
}
An abstract method contains the abstract keyword,
its name, parameters if required, and its return type.
abstract showDetails(): void;
Notice that there is no method body.
abstract class Animal {
abstract makeSound(): void;
}
class Dog extends Animal {
makeSound(): void {
console.log("Dog barks");
}
}
const dog = new Dog();
dog.makeSound();
We create an object of the concrete child class rather than the abstract parent class.
A non-abstract child class must provide implementations for the abstract methods inherited from its parent.
abstract class Animal {
abstract eat(): void;
}
class Dog extends Animal {
eat(): void {
console.log("Dog eats");
}
}
An abstract class can contain normal methods with complete implementations.
abstract class Animal {
move(): void {
console.log("Animal is moving");
}
abstract makeSound(): void;
}
abstract class Animal {
move(): void {
console.log("Animal is moving");
}
abstract makeSound(): void;
}
class Dog extends Animal {
makeSound(): void {
console.log("Dog barks");
}
}
const dog = new Dog();
dog.move();
dog.makeSound();
An abstract class can also declare abstract properties.
abstract class Animal {
abstract name: string;
}
The child class must provide the property.
abstract class Animal {
abstract name: string;
}
class Dog extends Animal {
name: string = "Dog";
}
const dog = new Dog();
console.log(dog.name);
An abstract class can have a constructor. The constructor is used when a child class is created.
abstract class Person {
constructor(public name: string) {
}
}
abstract class Person {
constructor(public name: string) {
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
const student = new Student("Rahul");
console.log(student.name);
abstract class Employee {
abstract calculateSalary(): number;
abstract showRole(): void;
displayCompany(): void {
console.log("ABC Company");
}
}
abstract class Employee {
abstract calculateSalary(): number;
abstract showRole(): void;
}
class Manager extends Employee {
calculateSalary(): number {
return 50000;
}
showRole(): void {
console.log("Manager");
}
}
Abstract classes are commonly used as base classes in inheritance.
abstract class Vehicle {
abstract start(): void;
}
class Car extends Vehicle {
start(): void {
console.log("Car started");
}
}
abstract class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
abstract showDetails(): void;
}
class Student extends Person {
showDetails(): void {
console.log("Student:", this.name);
}
}
The child class provides its own implementation of an abstract method.
abstract class Animal {
abstract sound(): void;
}
class Cat extends Animal {
sound(): void {
console.log("Cat meows");
}
}
An abstract class can provide common logic while leaving specific behavior to child classes.
abstract class Payment {
process(): void {
console.log("Processing payment");
}
abstract pay(): void;
}
class CardPayment extends Payment {
pay(): void {
console.log("Paid using card");
}
}
abstract class Shape {
abstract area(): number;
display(): void {
console.log("This is a shape");
}
}
class Rectangle extends Shape {
constructor(
private width: number,
private height: number
) {
super();
}
area(): number {
return this.width * this.height;
}
}
const rectangle = new Rectangle(10, 5);
rectangle.display();
console.log(rectangle.area());
| Feature | Normal Class | Abstract Class |
|---|---|---|
| Create Object | Yes | No |
| Normal Methods | Yes | Yes |
| Abstract Methods | No | Yes |
| Inheritance | Yes | Yes |
Both abstract classes and interfaces can define structures that other classes must follow, but they are used differently.
extends.abstract class Employee {
constructor(
public name: string
) {
}
showName(): void {
console.log("Name:", this.name);
}
abstract calculateSalary(): number;
}
class Developer extends Employee {
constructor(
name: string,
private salary: number
) {
super(name);
}
calculateSalary(): number {
return this.salary;
}
}
const developer = new Developer(
"Amit",
40000
);
developer.showName();
console.log(
developer.calculateSalary()
);
super() in a derived constructor.Abstract classes can be useful when different services, components, or application objects share common behavior but need different implementations for some operations.
abstract class ApiService {
log(): void {
console.log("API service");
}
abstract fetchData(): void;
}
class UserService extends ApiService {
fetchData(): void {
console.log("Fetching users");
}
}
abstract class to define a base class.abstract for methods or properties that child classes must provide.extends.super() to call the parent constructor.abstract class Employee {
constructor(
public name: string,
protected department: string
) {
}
showDetails(): void {
console.log("Name:", this.name);
console.log("Department:", this.department);
}
abstract calculateSalary(): number;
abstract getRole(): string;
}
class Developer extends Employee {
constructor(
name: string,
department: string,
private salary: number
) {
super(name, department);
}
calculateSalary(): number {
return this.salary;
}
getRole(): string {
return "Software Developer";
}
}
const developer = new Developer(
"Rahul",
"IT",
50000
);
developer.showDetails();
console.log(
"Role:",
developer.getRole()
);
console.log(
"Salary:",
developer.calculateSalary()
);
In this example, Employee provides common
functionality and defines two abstract methods.
Developer inherits from it and implements
those methods.
abstract keyword.extends to inherit from an abstract class.super() is used to call the parent constructor.Question: Which keyword is used to define an abstract class in TypeScript?