user3019483
user3019483

Reputation: 67

Is it a good practice to have an abstract parent and an abstract child classes in python?

I have 2 level inheritance, such that the parent is an abstract class that initialize common things for its first level children. In addition each first level child is also a base abstract class that initialize additional unique parameters for the second (and last) level inheriting classes

The classes will look similar to the following:

The abstract parent:

class AbstractParent(metaclass=ABCMeta):
    def __init__():
        <initialize common stuff>

    @abstractmethod
    def abstract_method():
        pass

First level abstract child:

class AbstractChild(AbstractParent, metaclass=ABCMeta):

    def __init___():
        AbstractParent.__init__(self)
        <initialize common stuff relevant for AbstractChild>

    @abstractmethod
    def abstract_method():
        pass

The second and last level child:

class Child(AbstractChild):

    def __init___():
        AbstractChild.__init__(self)

    def abstract_method():
        <implementation of something...>

Is it good practice to have multi-level inheritance such that multiple levels are abstract similar to what I presented above?

Thanks.

Upvotes: 0

Views: 447

Answers (1)

DSC
DSC

Reputation: 1153

If the class system of your projects requires it, then it's good practice. It all depends on what you're trying to implement.

For example. You could have a school that offers Courses. These courses can be Cooking and Painting. Now for cooking we have different styles Italian, Greek,....

Course and Cooking can be abstract here since we'll never have to create an instance of a generic Course.

Is this good practice? That all depends on the contents of your classes. Does Cooking contain extra information that Course doesn't have? Maybe a list of ingredients?

However we may want Cooking not to be an abstract class. Maybe a general cooking class is also possible.

It all depends on what you're trying to achieve.

Upvotes: 1

Related Questions