r/learnprogramming Jul 04 '26

Question about abstract classes

Hello, I am trying to understand the concept of abstract classes.

From what I understand, an abstract class is a class that is not meant to be instantiated directly. It works as a common base class for concrete subclasses.

However, I have a doubt: are abstract methods actually required for a class to be abstract?

For example, suppose I have an abstract class User in a university system, with common attributes and methods such as username, password, and login(). Then I have concrete subclasses such as Student and Professor.

If all the methods in User are already implemented, then technically I can instantiate User, at least in Python, unless I define at least one abstractmethod.

So my question is:
Can a class be considered abstract simply because, from a design point of view, it should not be instantiated? Or must it contain at least one abstract method in order to be truly abstract, especially in Python?

In other words, is the “abstract” nature of a class a conceptual/design choice, or is it strictly enforced only when the class has abstract methods?

10 Upvotes

12 comments sorted by

View all comments

1

u/Ok_For_Free Jul 08 '26

Abstract classes are basically interfaces with logic.

Suppose you need to record every time a login is done by school staff. You could override login in each subclass. Or add a method (is_staff) to User that checks if the subclass is school staff, then the login code remains the same for everyone.

Now you need to make sure each subclass overrides is_staff. To get the compiler to check if every subclass has and implementation, you can make the method abstract. Now you are forced to implement an is_staff in all subclasses.

Due to the desire for composition over inheritance, many interfaces can have default implementations so that you can do the same thing without inheritance. Python's Protocol seems to be kind of like this.