
上QQ阅读APP看书,第一时间看更新
What can go wrong with inheritance?
We are told that ducks can walk and quack. So we implement those behaviors in our parent class, namely Duck:


We start out with Mallard and BlueBilled ducks. We are able to reuse the walk() and quack() methods via inheritance.
Next, we hear that ducks can fly. So we implement the fly behavior in our Duck class and all the child classes inherit this new behavior:
All is well until we add Pekins ducks to our flock.
The problem that we did not account for in our original design is that most domestically bred ducks cannot fly:


The good news for us is that this sort of a design flaw is not even a possibility in Go!
The way we model behavior in Go is by using interfaces (Go does not support inheritance).