File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Which is better is-a or has-a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which is better is-a or has-a" Watch "Which is better is-a or has-a" New topic
Author

Which is better is-a or has-a

Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Hi,

I have one class B and i want to use some artifacts which are available in class A like methods and instance variables.
I just want to discuss that in this situation which is better is-a or has-a

Thanks,
Ram
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Hi Ram,

which one is better depends a lot on your requirements and a concrete example. Like so often there's not only one best solution which fits every problem.

But in general I'd say the "has-a" relationship, i.e. composition, is to prefer because it's easier to make changes to a system with composition later. If you have an inheritance hierarchy (the "is-a" relationship) things get more complicated if you have to make bigger changes to your existing classes. In particular if you have a deep inheritance tree things can even get worse.

Anyway, as I said it depends on the situation. Of course there are also many cases where inheritance is the right solution and it's not always easy to decide which one to use even if you get more experienced.

Marco
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Questions like this always make me ask "Which is better - a hammer or a wrench?"

The answer to my question is "it depends on what you want to do".

For your case... Is your class B really an A? In other words, in all possible scenarios, is it valid to use a 'B' any place you would use an 'A' (assuming I don't have this backwards for your case)?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Victor Ewert
Greenhorn

Joined: May 28, 2009
Posts: 17
Without knowing any more details, I would say in your case you should use 'has-a', by which I mean class B would have an instance of class A in it and you could use it.

'is-a' should only be used if class B is really is a type of class A. Example 'is-a' would be used if you have a Person class, and then you want to create an Employee class, then I would make Employee as a sub-class of Person since an Employee 'is-a' Person.

I hope this answers your question.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
It depends upon the situation. Without more information about your classes its impossible to say. The decision is often taken based upon a natural relationship between the classes

i.e its 'better' for:
Car has an Engine
Ferrari is a Car

Rajkishore Pujari
Ranch Hand

Joined: Sep 03, 2005
Posts: 46
Hi Ram,

In most situations HAS-A relationship is recommended over IS-A. You need IS-A relation if you think Class B is just an extension or more specialized form of Class A with more attributes or specific implementations.

Design rule: "Composition is preferred over Inheritance"

So for example: We have following classes

Class "Automobile".
Class "Engine"
Class "Car"
Class "Jeep"

Here are the relationships

Automobile HAS-A Engine
Car IS-A Automobile (Car extends Automobile)
Jeep IS-A Automobile (Jeep extends Automobile)

Let me know if you need more explanation.

Thanks,
Rajkishore.
Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Thanks a lot to all. I got my answer , that composition is good over inheritance.
Thanks to all again.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Ram Chhabra wrote:Thanks a lot to all. I got my answer , that composition is good over inheritance.

I don't believe anybody said that. What they said was "It depends". There are plenty of times when inheritance is the correct solution. It is a 100% acceptable way to go... in the correct situation.

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
fred rosenberger wrote:Questions like this always make me ask "Which is better - a hammer or a wrench?"

The answer to my question is "it depends on what you want to do".

For your case... Is your class B really an A? In other words, in all possible scenarios, is it valid to use a 'B' any place you would use an 'A' (assuming I don't have this backwards for your case)?



Sometimes a wrench is my hammer when it needs to be

SCJA
~Currently preparing for SCJP6
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ram Chhabra wrote:Thanks a lot to all. I got my answer , that composition is good over inheritance.
Thanks to all again.


Composition is preferred over inheritance when it makes sense. Someone used a car analogy, so I will go with that.

Let's look at an engine. It has many parts (pistons, belts, oxygen sensors, etc.). Now we wouldn't say that an engine IS-A piston, but rather an engine HAS-A piston.

On the same token, there are many different kinds of engines (gas, diesel, V-8, V-12). We wouldn't say an engine HAS-A V-12, or an engine HAS-A gas. We would say a V-12 IS-A(n) engine, or the gas type IS-A(n) engine.

I had to add the (n) to make it an. Sometimes I am a grammar freak.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is better is-a or has-a
 
Similar Threads
Overloading
static methods
JSP and JNI
adding big decimal numbers
Converting base clas to derived class