F
F
frontjss2020-03-18 14:27:37
OOP
frontjss, 2020-03-18 14:27:37

Why abstractions in OOP?

I understood how to implement an abstract class

But the essence is not clear to me, what can we do with it that we cannot do with others?

Please explain the essence of it in practice

Thank you very much!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
HemulGM, 2020-03-18
@HemulGM

An abstract class is needed in order to provide two or more other classes that will inherit it with the same access to some methods without implementing these methods in an abstract class.
For example, you have two classes "apple" and "orange".
And now, you want to "eat" them.
You call "apple1.eat" and "orange1.eat". Everything's OK. But, these two objects are not from the same ancestor, and it is impossible to say exactly that both objects actually have a "eat" method.
If this is clear, then let's move on.
An abstract method allows you NOT to write a method definition, but only its title.
I don't know exactly how it is in python. But, in an ordinary class, you must describe the method (let's say leaving it empty) and then other classes inherit from it. Whilein an abstract class, the implementation of the method is not needed , it is enough to say that the method is there and it must be implemented in all inherited classes, and also, you cannot create an object based on the abstract class .

S
Saboteur, 2020-03-18
@saboteur_kiev

https://en.wikipedia.org/wiki/%D0%90%D0%B1%D1%81%D
... ), which we take out into an abstract class and inherit from it

V
Vasily Bannikov, 2020-03-19
@vabka

Абстра́кция (лат. abstractio — отвлечение) — теоретическое обобщение как результат абстрагирования.

Example close to reality:
There is an array of numbers and a queue of messages.
You need to make two filter functions - the first one will pull out only even numbers that are greater than 0 from the array, and the second one will pull out only fresh messages with high priority from the queue.
Variant without abstractions:
we write two independent functions for each type of object.
Abstraction option:
  1. We create an abstraction for our task: "Given a collection of elements, you need to create a new collection and place in it only those elements that satisfy some given condition."
  2. We select two new abstractions from the condition: Collection of elements and Condition (predicate) .
  3. We create a function that will take a collection of elements and some predicate as an argument
  4. We implement the predicate interface - "the number is even and greater than 0" and "the message is fresh and important"

Result: We have invented the filter function
const array = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const predicate = num => num > 0 && num % 2 === 0;
const result = array.filter(predicate);

The abstraction option looks more complicated (it is) and makes you think more at the design stage, but it allows you to avoid code duplication and, sometimes, make it easier to work with the code.
As an exercise, you can try to implement such a function in circumstances where you don't have higher-order functions, and arrays and queues don't have some common interface to iterate over each element.
Hint: this is where the "adapter" and "strategy" patterns come in handy ( VicTHOR has already mentioned a good pattern book )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question