Skip to main content

Abstract Class

In Turkish
Soyut Sınıf
Pronunciation
AB-strakt KLASS
Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/abstract-class

In short

An abstract class is a class that can't be instantiated on its own; it shares code with its subclasses and declares methods that each subclass must implement.

What is an abstract class?

An abstract class is a class that exists only to be extended. You can't create an object from it directly; other classes inherit from it and fill in what is missing. It can hold everything a normal class has, such as fields, a constructor and fully written methods, plus abstract methods: methods with a name and parameters but no body, which every subclass must implement unless it is abstract too.

Java, C#, Kotlin and TypeScript mark such a class with the abstract keyword, and the compiler rejects both new Shape() and any subclass that leaves an abstract method out. Python gets the same effect from its standard abc module: a class that inherits from ABC and marks methods with @abstractmethod can't be instantiated until a subclass implements them. C++ has no keyword; a class with at least one pure virtual function, written with = 0, is abstract.

Abstract classes suit a family of types that share real code. A Shape class can write describe() once and leave area() abstract, so Circle and Square each supply only their own formula. This is the template method pattern: the base class fixes the steps of an algorithm and lets subclasses fill in some of them, like a printed form where some fields are already filled in and the rest are left blank for each person.

The classic question is abstract class vs interface. An interface is a contract with no state, and a class can implement many; an abstract class can also carry fields, a constructor and shared code, but in Java, C# and TypeScript a class can extend only one. Since Java 8 and C# 8, interfaces can contain default method bodies, which blurs the line, yet instance fields and constructors still belong only to classes. A good rule: use an interface to describe what something can do, and an abstract class to share how related types do it.

Key takeaways

  • An abstract class can't be instantiated; it exists to be extended.
  • It can mix fields and fully written methods with abstract methods that have no body.
  • Every subclass that isn't abstract itself must implement all the abstract methods.
  • In Java, C# and TypeScript a class can implement many interfaces but extend only one class.

Example

An abstract class with shared code and an abstract method in TypeScripttypescript
abstract class Shape {
  constructor(public readonly name: string) {}
  abstract area(): number;            // no body: each subclass must write one
  describe(): string {                // shared code that every subclass inherits
    return `${this.name} has an area of ${this.area().toFixed(1)}`;
  }
}

class Circle extends Shape {
  constructor(private radius: number) { super("circle"); }
  area() { return Math.PI * this.radius ** 2; }
}

new Circle(2).describe(); // "circle has an area of 12.6"
// new Shape("blob");     // error: Cannot create an instance of an abstract class.

Readers ask

What is the difference between an abstract class and an interface?

An interface only declares what a type can do, and a class can implement several. An abstract class can also hold fields, a constructor and shared method code, but a class can extend only one. Use an interface for a capability and an abstract class for behavior shared by a family of related types.

Can an abstract class have a constructor?

Yes. You can't call it with new directly, but each subclass calls it, for example with super(...), to set up the fields that the abstract class defines.

Does Python have abstract classes?

Not as a keyword, but the standard abc module provides them. Inherit from ABC and mark methods with @abstractmethod, and Python raises a TypeError if you try to create an object before every abstract method is implemented.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

More

Settings