Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"It's implied that the relationship was used because real ducks are real animals, not because Duck inheriting from Animal is a good idea."

I just do not understand this objection. If you wrote Sim Farm, it's quite possible you'd want Duck to inherit from Bird and Cow and Pig to inherit from Mammal, and both Bird and Mammal to inherit from Animal.

In the course of caring for your farm, you have to fix tractors, grow crops, and care for animals.

All Animals must be fed and will starve if they don't. Why would you code "needs food" on every individual animal class? Birds can get avian flu, and Mammals can get rabies; the pigs can even get rabies from the dogs. Why would you code "can get rabies" on Dog and Pig?

Maybe you don't always want to use Sim Farm as your example for teaching OO, but a lot of working code does model real-world items. Animals and cars and such are no worse than files as objects to model, and have the advantage that non-programmers already know something about them. Hence they can focus on the OO concepts and not on the domain logic.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: