java - Composition over Inheritance Real life example -
this question has answer here:
- prefer composition on inheritance? 34 answers
i trying understand phrase "favor composite on inheritance".
could give me real life example why composition better inheritance.
also if there drawbacks?
inheritance when class type of , shares base functionality , properties of other classes of same type.
e.g vehicle -> car
. vehicle might have properties (composition) weight
or topspeed
classes inherited share.
composition specific makeup of properties of class, e.g. car -> int numdoors
. methods represent composition, overridden child class implementations in case of inheritance. example, drive
method of vehicle
might have common implementation related gas consumption, while car
override method perhaps first calling base method of vehicle
(super.drive()
), , further specifying own specific process.
also see http://en.wikipedia.org/wiki/composition_over_inheritance.
Comments
Post a Comment