Can we instantiate an abstract class?

No, we cannot instantiate an abstract class directly. Abstract classes are meant to serve as blueprints for other classes. They define common behaviors or properties through abstract methods and concrete methods but are incomplete on their own and cannot be instantiated.

Why Can't We Instantiate an Abstract Class?

Abstract classes often include one or more abstract methods—methods that are declared but not implemented. Since the behavior of these methods is undefined, it wouldn't make sense to create an instance of a class that has incomplete functionality.

Instead, abstract classes must be subclassed, and their abstract methods must be implemented in the derived (concrete) classes.

How Abstract Classes Work in Different Languages

In Python

In Python, abstract classes are created using the abc module. A class with at least one @abstractmethod is considered abstract and cannot be instantiated.

Example:

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass class Dog(Animal): def make_sound(self): return "Woof" # Attempt to instantiate an abstract class animal = Animal() # Error: Can't instantiate abstract class Animal dog = Dog() # Works because Dog provides an implementation for make_sound print(dog.make_sound()) # Output: Woof

Explanation:

  • Animal is an abstract class with an abstract method make_sound.
  • Attempting to instantiate Animal directly raises a TypeError.

In Java

In Java, abstract classes are declared using the abstract keyword. They cannot be instantiated, but they can have constructors, concrete methods, and fields.

Example:

abstract class Shape { abstract double area(); // Abstract method } class Circle extends Shape { private double radius; Circle(double radius) { this.radius = radius; } @Override double area() { return Math.PI * radius * radius; } } // Attempt to instantiate an abstract class Shape shape = new Shape(); // Error: Shape is abstract; cannot be instantiated Shape circle = new Circle(5); // Works System.out.println(circle.area()); // Output: 78.53981633974483

Explanation:

  • The Shape class cannot be instantiated because it is abstract.
  • The Circle class provides an implementation for the area method, allowing it to be instantiated.

In C++

In C++, abstract classes are created by defining at least one pure virtual function using = 0. These classes cannot be instantiated directly.

Example:

#include <iostream> #include <cmath> using namespace std; class Shape { public: virtual double area() const = 0; // Pure virtual function }; class Circle : public Shape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return M_PI * radius * radius; } }; // Attempt to instantiate an abstract class // Shape shape; // Error: Cannot instantiate abstract class Circle circle(5); cout << circle.area() << endl; // Output: 78.5398

Explanation:

  • Shape is abstract due to the pure virtual function area.
  • Circle overrides area, allowing instances of Circle to be created.

Exceptions and Special Cases

  1. Factory Methods:

    • Abstract classes can provide a method to create instances of their concrete subclasses.
    • Example in Python:
      from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass @staticmethod def create_animal(type): if type == "dog": return Dog() elif type == "cat": return Cat() class Dog(Animal): def make_sound(self): return "Woof" class Cat(Animal): def make_sound(self): return "Meow" animal = Animal.create_animal("dog") print(animal.make_sound()) # Output: Woof
  2. Partially Implemented Abstract Classes:

    • Abstract classes can contain concrete methods, which can be used by subclasses.
    • Example in Java:
      abstract class Animal { void sleep() { System.out.println("Sleeping..."); } }
  3. Metaprogramming in Python:

    • Python’s dynamic nature allows bypassing the restriction by modifying class behavior at runtime, though this is strongly discouraged.

Summary

  • Abstract classes cannot be instantiated because they are incomplete and meant to be extended by subclasses.
  • They are used to define shared behavior and enforce implementation of abstract methods in subclasses.
  • Concrete subclasses must implement all abstract methods before they can be instantiated.
  • Abstract classes improve design by promoting code reusability, consistency, and extensibility in object-oriented programming.
TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the salary of a full stack developer in PayPal?
What is software testing in QA?
What is the first product of OpenAI?
What is polymorphism, what is it for, and how is it used?
What are the tips for coding interviews at fintech companies?
Who is the CEO of Microsoft?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.