What are the benefits of interfaces and abstract classes?

Possible duplicates:
assigning an interface to classes
What is the difference between an interface and an abstract class?

Hi I am a php programmer. any authority can explain the advantage of using an interface and an abstract class.

+3
source share
1 answer

The main advantage of the interface is that it allows you to determine the protocol that will be implemented so that the object has some kind of behavior. For example, you might have a Comparable interface with a comparison method for classes to implement, and each class that implements it will have a standardized comparison method.

. , , , :

abstract class Animal {
    abstract protected function eat();
    abstract protected function sleep();
    public function die() {
        // Do something to indicate dying
    }
}

eat() sleep() , (, , ..), Animal -. ( ), . 1.) , Animal, 2.) Animal s. , Animal die().

+11

Source: https://habr.com/ru/post/1783683/


All Articles