Abstract Classes in Python

Yang Zhou
TechToFreedom
Published in
2 min readMay 18, 2020

--

Photo by Photo Boards on Unsplash

Introduction

Abstract Class is a very important concept of object-oriented programming. It is a good practice of DRY (Don’t Repeat Yourself) principle. In a large project, code duplication is approximately equal to bug reuse and one developer is impossible to remember all classes’ details. Therefore, it’s very helpful to use an abstract class to define a common interface for different implementations.

An abstract class has some features, as follows:

  • An abstract class doesn’t contain all of the method implementations required to work completely, which means it contains one or more abstract methods. An abstract method is a method that just has a declaration but does not have a detail implementation.
  • An abstract class cannot be instantiated. It just provides an interface for subclasses to avoid code duplication. It makes no sense to instantiate an abstract class.
  • A derived subclass must implement the abstract methods to create a concrete class that fits the interface defined by the abstract class. Therefore it cannot be instantiated unless all of its abstract methods are overridden.

In a nutshell, an abstract class defines a common interface for a set of subclasses. It provides common attributes and methods for all subclasses to reduce code duplication. It also…

--

--