What are the alternatives to inheritance? | Eklavya Online

What are the alternatives to inheritance?

Delegation is an alternative to inheritance.

Delegation denotes that you include an instance of any class as an instance variable, and forward messages to the instance.

It is safer than inheritance because it ceases you to think about forwarded message , because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense.

On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).