Abstract Classes in Python (original) (raw)

In Python, an abstract class is a class that cannot be instantiated on its own and is designed to be a blueprint for other classes. Abstract classes allow us to define methods that must be implemented by subclasses, ensuring a consistent interface while still allowing the subclasses to provide specific implementations.

When to Use Abstract Classes

Abstract classes are useful when you want to:

Abstract Base Classes

An Abstract Base Class (ABC) defines methods that must be implemented by its subclasses, ensuring that the subclasses follow a consistent structure. ABCs allow you to define common interfaces that various subclasses can implement while enforcing a level of abstraction.

Python provides the abc module to define ABCs and enforce the implementation of abstract methods in subclasses.

**Example: This example shows an abstract class Animal with an abstract method sound() and a concrete subclass Dog that implements it.

Python `

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass

class Dog(Animal): def sound(self): return "Bark"

dog = Dog() print(dog.sound())

`

**Explanation:

Abstract Methods

Abstract methods are methods that are defined in an abstract class but do not have an implementation. They serve as a blueprint for the subclasses, ensuring that they provide their own implementation.

**Example: This example shows that trying to instantiate an abstract class directly raises an error.

Python `

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass

animal = Animal() # Raises: TypeError

`

**Explanation: make_sound() is an abstract method in the Animal class, so it doesn't have any code inside it. If you try to instantiate Animal directly, Python will raise a TypeError since the method is not implemented.

Concrete Methods

**Concrete methods are methods that have full implementations in an abstract class. These methods can be inherited by subclasses and used directly without needing to be redefined.

**Example: This example defines a concrete method move() alongside an abstract method.

Python `

class Dog(Animal): def make_sound(self): return "Bark"

dog = Dog() print(dog.move()) # Uses concrete method from Animal print(dog.make_sound())

`

**Explanation: Here, Dog inherits the concrete method move() from Animal without redefining it, while also implementing its required abstract method make_sound().

Abstract Properties

**Abstract properties work like abstract methods but are used for properties. These properties are declared with the @property decorator and marked as abstract using @abstractmethod.

Abstract properties enforce that a subclass provides the property’s implementation. They are especially useful when you want all subclasses to have a certain attribute (like species, category or type).

**Example: This example defines species as an abstract property, which subclasses must implement.

Python `

from abc import ABC, abstractmethod class Animal(ABC): @property @abstractmethod def species(self): pass

class Dog(Animal): @property def species(self): return "Canine"

Instantiate the concrete subclass

dog = Dog() print(dog.species)

`

**Explanation:

Abstract Class Instantiation

Abstract classes cannot be instantiated directly. This is because they contain one or more abstract methods or properties that lack implementations. Attempting to instantiate an abstract class results in a TypeError.

**Example: This example shows the error raised when trying to instantiate an abstract class.

Python `

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass

animal = Animal()

TypeError: Can't instantiate abstract class Animal with abstract methods make_sound

`

**Explanation:

**Note: If a subclass does not implement all abstract methods or properties from its parent, it also remains abstract and cannot be instantiated.