A
A
Anna B2016-01-08 21:27:13
PHP
Anna B, 2016-01-08 21:27:13

Why do we need abstract classes (PHP)?

I started to study OOP and I can’t figure out why abstract classes are needed if there are interfaces.
The interface, as I understand it, is needed, roughly speaking, for the following. We have, for example, objects of class Vegetable. And there is the Salad interface, which implements the Wash, Cut, Mix methods. And we can then apply this interface to some other classes, if all these methods are needed (for example, to objects of the Fruit class).
But about abstract classes, I don’t understand why they are needed, where and when to use them correctly. If possible, please give some simple examples that show that in some case it is necessary to use an abstract class, not an interface.
Thank you.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Anton Natarov, 2016-01-08
@HanDroid

Different languages ​​have different ways. For example, in Java, you can implement a bunch of interfaces, but you cannot implement multiple inheritance of no more than 3 heirs, and using interfaces solve this.
An interface is usually needed when only an interface is described (tautology). For example, one class wants to give another the ability to access some of its methods, but does not want to "reveal" itself. So it just implements the interface.
An abstract class is needed when you need a family of classes that have a lot in common. Of course, you can also use the interface, but then you will need to write a lot of identical code.
Example:An abstract class will certainly not be requested as an object. For example, an abstract class - Transport: But all its heirs will be a Car, a crane, a boat, an airplane, etc. For example, you know for sure that all vehicles will move. And you declare an abstract method (movement) in an abstract class, which will be needed by 100% of all heirs i.e. without movement, it is no longer transport, and the new heir will be obliged to implement it. Within the abstract class itself, there are other fields and properties that will be inherited. Well, for example, engine power (very roughly), or something that makes them related.

S
Sergey, 2016-01-08
Protko @Fesor

a typical use case for abstract classes is a basic implementation, reducing code duplication when a common solution appears, etc. For example, you have an interface with several methods, and only 2-3 of them are implemented differently. It is advantageous to move the common implementation into the base abstract class.

T
tasce, 2016-01-08
@tasce

Good answer: www.quizful.net/interview/php/abstract-class-inter...
And, of course, we read of. documentation: php.net/manual/ru/language.oop5.abstract.php

R
Robot, 2016-01-08
@iam_not_a_robot

In a simple way, this is a preparation.
I think it will clarify the situation a little:
myrusakov.ru/abstraktnye-klassy-php.html
lifeexample.ru/php-primeryi-skriptov/abstraktnyiy-...
And of course the article "Stop writing classes" habrahabr.ru/post/140581 ;)

U
utyfua, 2016-01-08
@utyfua

open Wikipedia: An abstract class in object-oriented programming is a base class that does not involve instantiation.
This means: we can create a site class and fill it with all the functions. it is useless to make such 2 or more copies ..

A
Alexander Litvinenko, 2016-01-08
@edli007

Well, everyone wrote about the general purpose here, but there is still a nuance called "late static binding", what it is, read a lot of letters, but a very important nuance in using abstract classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question