Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is better is-a or has-a

 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?

 
Victor Ewert
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to all. I got my answer , that composition is good over inheritance.
Thanks to all again.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic