When do we need to use Inheritance?

Basically, inheritance and composition both make one object contain another object.
Then when do we need to use inheritance not composition?
1. Need use polymorphism mechanism by upcasting. For c++, it's done by virtual function.
2. Need to access a class's some functions or member variables, but don't want to expose those to be public accessible.
3. Need to reuse code in an apparent inheritance relationship tree.

你可能感兴趣的:(inheritance)