Python
abstract classes
abstract properties
object-oriented programming
Python programming

How to create abstract properties in python abstract classes?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python supports abstract properties through the abc module. The basic pattern is to combine @property with @abstractmethod, which tells subclasses that a property must be implemented before the class can be instantiated.

The Basic Abstract Property Pattern

A read-only abstract property looks like this:

python
1from abc import ABC, abstractmethod
2
3
4class Shape(ABC):
5    @property
6    @abstractmethod
7    def area(self) -> float:
8        pass
9
10
11class Square(Shape):
12    def __init__(self, side: float):
13        self.side = side
14
15    @property
16    def area(self) -> float:
17        return self.side * self.side
18
19
20square = Square(3)
21print(square.area)

The important part is the decorator order:

  • '@property'
  • '@abstractmethod'

That defines a property whose implementation is required in concrete subclasses.

What Happens If You Do Not Implement It

If a subclass fails to implement the abstract property, Python keeps the subclass abstract and prevents instantiation.

python
1from abc import ABC, abstractmethod
2
3
4class Shape(ABC):
5    @property
6    @abstractmethod
7    def area(self) -> float:
8        pass
9
10
11class BrokenShape(Shape):
12    pass
13
14
15# BrokenShape() would raise TypeError

That is the whole point of the abstract base class: it enforces an interface contract.

Abstract Setter Example

Sometimes the contract includes not just reading a property, but setting it as well. In that case, define both the getter and the setter as abstract where appropriate.

python
1from abc import ABC, abstractmethod
2
3
4class Config(ABC):
5    @property
6    @abstractmethod
7    def name(self) -> str:
8        pass
9
10    @name.setter
11    @abstractmethod
12    def name(self, value: str) -> None:
13        pass
14
15
16class AppConfig(Config):
17    def __init__(self):
18        self._name = "default"
19
20    @property
21    def name(self) -> str:
22        return self._name
23
24    @name.setter
25    def name(self, value: str) -> None:
26        self._name = value
27
28
29cfg = AppConfig()
30cfg.name = "demo"
31print(cfg.name)

This pattern is useful when the API contract requires property-style access but still needs validation or state control.

Why Use an Abstract Property Instead of an Abstract Method

Use an abstract property when the subclass contract is about an attribute-like value rather than an action.

Good candidates include:

  • dimensions
  • configuration values
  • identifiers
  • computed attributes such as area, length, or is_ready

If the abstraction feels like “do something,” an abstract method is usually clearer. If it feels like “expose a value,” an abstract property is often the better interface.

Abstract Properties Can Be Computed

The property does not have to map to a stored attribute. It can be computed dynamically.

python
1from abc import ABC, abstractmethod
2
3
4class Employee(ABC):
5    @property
6    @abstractmethod
7    def annual_salary(self) -> float:
8        pass
9
10
11class HourlyEmployee(Employee):
12    def __init__(self, hourly_rate: float, hours_per_year: int):
13        self.hourly_rate = hourly_rate
14        self.hours_per_year = hours_per_year
15
16    @property
17    def annual_salary(self) -> float:
18        return self.hourly_rate * self.hours_per_year
19
20
21employee = HourlyEmployee(50, 2000)
22print(employee.annual_salary)

This is one reason abstract properties fit well with interface design. They say what value must be exposed, not how the subclass must store it.

Prefer Clear Contracts Over Cleverness

Abstract properties are useful, but not every attribute should become one. If the class contract does not actually need property semantics, a plain method or a documented attribute may be simpler.

Use abstract properties when:

  • subclasses must expose a consistent value-like API
  • property access is the natural interface
  • you want instantiation blocked until the contract is fulfilled

That keeps the abstraction intentional rather than decorative.

Common Pitfalls

The biggest mistake is getting the decorator order wrong. Put @property above @abstractmethod for the getter.

Another mistake is implementing the getter but forgetting the setter when the abstract contract includes one. In that case, the subclass remains abstract.

Developers also sometimes use abstract properties for values that would be clearer as ordinary methods. Not every required interface member needs property syntax.

Finally, remember that abstract base classes enforce structure at instantiation time, not at static type-check time only. A missing implementation causes a runtime TypeError when you try to create the subclass.

Summary

  • Use @property together with @abstractmethod to define an abstract property.
  • Subclasses must implement the property before they can be instantiated.
  • Add an abstract setter if the contract requires write access too.
  • Abstract properties are best for value-like interfaces, not action-like behavior.
  • Keep the interface clear: use property syntax when the abstraction is naturally attribute-shaped.

Course illustration
Course illustration