Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is Abstraction? | 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

bookWhat is Abstraction?

Abstraction is one of the fundamental concepts of OOP in Python. From a human perspective, all classes in code are abstract (we can't touch them). However, machines can operate with them. Nevertheless, it is possible to create an abstraction even for machines, making the class abstract for them as well.

In Python, an abstract class is a class that cannot be instantiated directly and is meant to serve as a blueprint for other classes. It provides a common interface or set of methods that derived classes are expected to implement.

Let's consider the example:

12345678910111213141516
from abc import ABC, abstractmethod class SomeClass(ABC): @abstractmethod def something(): print("Something") class NewClass(SomeClass): attribute = "Something" def something(self): print(self.attribute, "is the NewClass attribute") new_class = NewClass() # created new_class.something() instance = SomeClass() # raised an error
copy

The provided example demonstrates the creation of an abstract class called SomeClass, which serves as a blueprint for the derived class NewClass. An abstract class acts exclusively as a blueprint for its derived classes and cannot be directly instantiated.

In subsequent chapters, we will explore the ABC class, the @abstractmethod decorators, and delve into the structure and details of abstract classes.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
some-alt