P
P
pgamesorg2020-03-24 12:15:13
Python
pgamesorg, 2020-03-24 12:15:13

Why do we need abstraction in OOP?

Why is the pattern implemented through the abstraction tyts ?

Couldn't you just write these methods in a regular class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2020-03-24
@HemulGM

Can.
Why? So that objects are not created on the basis of an abstract class.
So that the interpreter prompts you that you have not described such and such a method (which is abstract).

M
Maxim Fedorov, 2020-03-24
@Maksclub

There are no interfaces in Python, so you have to declare an abstraction without behavior like this.
Why is abstraction needed - then, so that the code is not tied to a specific object, but tied to some boundaries of behavior (which is abstraction) - in this case, to some SUbject

class Observer(ABC):
    @abstractmethod
    def update(self, subject: Subject) -> None:
        """
        Получить обновление от субъекта.
        """
        pass

Now the Observer (all its descendants) in the parameters of the update() method accept ANY object of type Subject, that is, the code is tied to the abstraction, not the implementation of these subjects and does not know anything at all about their names and individual behavior, it only knows that any abstraction will arrive of type Subject....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question