龚城师 发表于 2013-8-16 16:48:56

[Oracle interview]What are some alternatives to inheritance?

陈西 发表于 2013-8-16 16:54:40

Delegationis an alternative to inheritance. Delegation means that you include aninstance of another class as an instance variable, and forward messages tothe instance. It is often safer than inheritance because it forces you tothink about each message you forward, because the instance is of a knownclass, rather than a new class, and because it doesn't force you to acceptall the methods of the super class: you can provide only the methods thatreally make sense. On the other hand, it makes you write more code, and it isharder to re-use (because it is not a subclass).
页: [1]
查看完整版本: [Oracle interview]What are some alternatives to inheritance?