Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Abstract Class | Polymorphism and Abstraction
In-Depth Python OOP
course content

Course Content

In-Depth Python OOP

In-Depth Python OOP

1. OOP Concepts
2. Inheritance
3. Encapsulation
4. Polymorphism and Abstraction
5. Magic Methods

Abstract Class

Abstract Class is a class that can't have instances but can have subclasses.

To create the abstract class, you need to import the ABC (Abstract Base Class) from the abc built-in library:

Also, you need to inherit from class ABC:

Now, we have the abstract class SomeClass, but we can create instances:

12345678
from abc import ABC class SomeClass(ABC): pass instance = SomeClass() instance.something = "Something" print(instance.something)
copy

In the example above, we created an instance of SomeClass because SomeClass does not have any abstract methods.

To create an abstract class, it should follow the following structure:

  1. The abstract class should inherit from the ABC class.
  2. The abstract class should define one or more abstract methods.

Note

A class is not considered abstract unless it has at least one abstract method and inherits from the ABC (Abstract Base Class) class.

We will describe abstract methods in the next chapter.

Everything was clear?

Section 4. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt