Article From:https://segmentfault.com/q/1010000011711144
Question:

The reverse subclass also needs to be rewritten. Why do we need to define this method in the parent class? Is it okay to not write?

abstract class ACanEat {
// Abstract methods need to be implemented in subclasses.
abstract public function eat($food);

public function breath(){

  echo "Breath use the air.<br />";

}
}

// HumanClass inherits the ACanEat abstract class.
class Human extends ACanEat {
// It is different from the implementation of Animal class.
public function eat($food){

echo "Human eating " . $food . "\n";

}
}

// AnimalClass inherits the ACanEat abstract class.
class Animal extends ACanEat {
public function eat($food){

echo "Animal eating " . $food . "\n";

}
}

$man = new Human();
$man->eat(“Apple”);
$man->breath(); // Breath method that shares the abstract class ACanEat with Animal
$monkey = new Animal();
$monkey->eat(“Banana”);
$monkey->breath();

Answer 0:

The abstract class is equivalent to the function of the template, in which the abstract method defined is used to enforce the implementation of the subclass.

eg.The base class of a database connection must declare an abstract function connect () whose subclasses, such as mysql, SQL server, inherit the parent class and implement their respective methods of connection.

Answer 1:

The abstract method in the parent class is a common property, and each animal has an eat method. When the parent class is defined, the child class must implement it. If you don’t think the eat is so common, you can’t leave it alone, so there may be some subclasses that don’t have eats.
After defining an abstract method in the parent class, there’s another advantage that you can call the method in the parent class and implement it in the child class, so you don’t have to call the method in the child class every time.
If you feel good, praise yourself.

Answer 2:

abstractClasses and interface classes usually use parent classes, which are used to constrain subclasses and follow certain specifications.

Answer 3:

When an abstract class is inherited, subclassAll abstract methods in the parent class must be defined.;In addition, the access control of these methods must be the same as that of the parent class (or more relaxed).

Code example:

<?php 

/**
* Abstract Class for Animal
*/
abstract class AnimalAbstract{

    abstract public function eat($food);

    public function run(){
        echo "It's running...";
    }
}

/**
* Class for Dog
*/
class Dog extends AnimalAbstract{

    public function eat($food){
        echo "It's eating bones";
    }

}

/**
* Class for Cat
*/
class Cat extends AnimalAbstract
{
    public function eat($food){
        echo "It's eating fish";
    }    
}

If the subclass does not implement the parent class method, it will report error.
Code example:

class Dog extends AnimalAbstract{

    // public function eat($food){
    //     echo "It's eating bones";
    // }

}

Error message:

Fatal error: Class Dog contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AnimalAbstract::eat) 

Official text:
PHPabstract class

Answer 4:

Abstract methods and interface methods are mainly used to constrain subclasses to be implemented according to a specific software structure, and are the medium of compulsory implementation of software architecture. abstract methods and interface methods play an important role in multi-person team cooperation development to ensure the software structure.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *