Registered User Registered User - 2 years ago 573
Java Question

java: inheritance

What are some alternatives to inheritance?

Answer Source

Effective Java: Favor composition over inheritance. (This actually comes from Gang of Four, as well).

The case he makes is that inheritance can cause a lot of unseemly side effects, if the extended class was not explicitly designed to be inherited. For example, any calls to super.someMethod() can lead you through unexpected paths of unknown code.

Instead, hold a reference to the class you would have otherwise extended, and delegate to it.

Here is a link to an interview with Erich Gamma where he talks about the concept a bit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download