简体   繁体   中英

Difference between abstract class extends and normal class extends


Is there any clear difference why to use abstract for extends if we can do same in with the normal class excepts it doesnt provide the contract for eg.

abstract class Survivalneeds {

abstract public function eat(); // everyone eats but different foods which would probably work as contract

public function breathe() {

// everyone inhale o2 exhale co2 only for animals

}

}

Now

class human extends Survivalneeds {

  protected function eat() {
//sometimes eat goat
// contract            
}

breathe()// already extending having same functionality  inhale o2 and exhale co2



}

class goat extends Survivalneeds{

 protected function eat() {
//wee eat greens
// contract            
}

breathe()// already extending having same functionality  inhale o2 and exhale co2

}

Now the same functionality can be granted by normal class by extending except the contract method and for contract we could use interface also.

您说的是它的正确继承在两种情况下都有效,但是Abstract类的想法是它由x类共享一些通用逻辑,这些逻辑扩展了此功能,但不能自己实例化,因为它没有意义(也许您只希望您的系统中有汽车的类型,而没有品牌的普通汽车)

Also if you will use regular class and interface you will be forced to create stub in a class in order to follow the contract. So you will be able to create the instance of the class. And just imagine you will use this common function in your upper class.

interface  Crashable{

    function crash();
}
class Car implements Crashable{
    function crash(){}
    function getCrashParams(){
        return $this->crash();
    }
}
class Volvo extends Car{
    function crash(){
        parent::crash(); // will be OK  that it's not right
        //.. specific params
        return $params;
    }
}
class Saab{
    function crash(){
        //.. specific params
        return $params;
    }
}

$car = new Car(); // will be ok, that it's not right 

//getCrashParams() function in a Car will use the local version of the crash() and not the function of it's child that will kill the data flow

You should use an interface whenever you have a need for a contract. You should use abstract class in case there's a common functionality for some simmilar classes and you don't want to repeat the code ( DRY :). Of course, it is always better to use composition, but this is not the time for this discussion :)

The problem with your code (with Survivalneeds class) is the fact the class from one side is responsible for the contract (breathe and eat methods) and from another is responsible for providing common functionality. You could change your code in following way:

interface Survivor {
    public function eat();
    public function breathe();
}

abstract class Survivalneeds implements Survivor {
    public function breathe() {
        // method's body
    }
}

With such implementation responsibilities are splitted. Also it is clear that all classes that will extend Survivalneeds will need to as well fulfill Survivor contract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM