File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OO Approaches: Favor object composition over inheritance.

 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

James Cooper (http://www.patterndepot.com/put/8/JavaPatterns.htm) said:

"Favor object composition over inheritance...

...the other major concept you should recognize is that of object composition.

This is simply the construction of objects that contain others: encapsulation of several objects inside another one. While many beginning OO programmers use inheritance to solve every problem, as you begin to write more elaborate programs, the merits of object composition become apparent. Your new object can have the interface that is best for what you want to accomplish without having all the methods of the parent classes."

Does anybody have a code example to illustrate this premise?

Cheers,

Simon
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A prominent example is java.util.Stack. Having it inherit from Vector was an error, it should have used composition.

If they had used composition, Vector now could use an ArrayList, LinkedList or even an array internally. Because they choose to use inheritance, they can't make that change, because it could break existing clients.

Additionally, the interface of Stack is poluted with methods from Vector that actually don't make sense for a Stack.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

As an aside, this heuristic actually only applies to interface inheritance. Pure implementation inheritance (such as C++ private inheritance) doesn't have that problem. Java doesn't support pure implementation inheritance, though - you always also inherit the superclasses interface.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found another scary example in my company. Somebody started out with a base class that could connect to a database. He extended it with another class that could execute a query. He extended that with any class in the system that needed database data. So now any business object that happens to need data "is a" database aware thingy plus whatever business logic it adds.

It would have been much better to have a data accessor that any other object could "have" or use. Then a business object could extend an appropriate abstract business class instead of always extending a database thing. And if "data accessor" was an interface we could plug in data accessors for LDAP and MQ-Series and flat files without breaking the business classes.

The "is a" heuristic is not 100% reliable for inheritance, but it's a good sanity check now and then. SecurityManager "is a" DatabaseAccessor just doesn't sound right.
 
Costa lamona
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is there a difference between "Has-a" and composition ?
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a difference between "Has-a" and composition ?

Maybe. It depends on how you are using the terms.

If you search around for discussions of "composition" vs "aggregation" you will find lots of interesting discussion around the finer points of this issue.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I dislike the "has-a" notion as a design notion, because I think it's misleading. I have less of a problem talking about an entity having another entity from an analysis point of view. Whether the "has-a" relationship than is implemented as composition or something else then is a technical decision - "has-a" really isn't precise enough to be of much help when you think about design, in my opinion.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic