tags:

views:

279

answers:

7

I was just reading through the book and it had the terms, "HAS-A" and "IS-A" in it. Anyone know what they mean specifically? Tried searching in the book, but the book is 600 pages long. Thanks!

+6  A: 

Has-a means that the Class in question 'has a' field of a type.

Is-a means that the Class extends from a superclass or implements an interface. The instanceof operator would return true if tested against a class in the this case.

akf
Not so much lmgtfy as lmwtfy... :P
Beau Martínez
+18  A: 

A Car has-a Wheel.

A Sparrow is-a Bird.

Academically, the terms are used to decide between composition and inheritance.

Anon.
Nice succinct answer.
Beau Martínez
+1  A: 

This isn't java specific, it's OO specific.

IS-A means you can have a class that "is a" something. Like a student "IS-A" person. This is used to describe one object being a subclass of another.

Sometimes there isn't a IS-A relationship between two classes, but a "HAS-A" is more appropriate. Example, a chair "HAS-A" leg. or several. this is called aggregation, as opposed to inheritance.

I wont go into the details of when to use each, because it depends on how the classes are being used and even if that is known there is so much to consider, a new question for a specific case would be more appropriate.

Chris H
Very nice, I wondered who was going to add aggregation in their answer.
Secko
And yet even things like 'Student IS-A person' tend to be very problematic, for instance in cases when a Student happens to later become a Professor. I generally prefer not to attack OO problems based upon the structure of things, but rather on their behaviors and how they transform data. I almost never use inheritance for that kind of structural work, only for values (a Name is a type of String), or for reusing template code.
kyoryu
A: 

Is a = Special kind of e.g. Car is special kind of Vehicle.

Has a = It physically has something, e.g Car has an engine.

fastcodejava
+4  A: 

This is object-oriented programming and UML terminology, not Java-specific. There are actually three cases you should be aware of:

  1. A House is a Building (inheritance);
  2. A House has a Room (composition);
  3. A House has an occupant (aggregation).

The difference between (2) and (3) is subtle yet important to differentiate. Together they are forms of association. What's the difference? Composition implies the child object cannot live out of the context of the parent (destroy the house and rooms disappear) whereas aggregation implies the child can exist on its own (destroy the house and the occupant goes elsewhere).

cletus
+1 for the distinction between composition and aggregation. Another way to say is aggregation is a loose form composition.
fastcodejava
I think there's a 'missing' relationship - 'collaborates with.' That seems to line up relatively well with your 3rd definition. The primary difference that I see between definitions 2 and 3 is one of ownership.
kyoryu
A: 

as cletus points out, is-a is different. but be careful with has-a. this can mean composition (lifetime responsbility), aggregation (part-of something), or simply uses-a (has a reference to, knows how to build one or find one). the latter is just an association.

Ray Tayek
For mutable entities, 'uses-a' is probably the most important relationship, and the one that gets the least thought.
kyoryu
+1  A: 

IS-A, HAS-A etc. are not really very OO. Instead, Liskov substitution principle is OO.

Uncle Bob sheds some light on the history of IS-A at http://www.hanselminutes.com/default.aspx?showID=163

Robert C. Martin: The word "ISA" crept into our vocabulary and by the way that's one word ISA, it crept into our vocabulary through a circuitous route and became very important in object oriented circles, but it didn't start out that way. It crept in the 80's through the AI crowd who had created these wonderful knowledge nets, you might remember this, all the hype about Artificial Intelligence in the late 80's, early 90's, and then created these structures that would walk knowledge nets these inference engines, and the relationships between the entities and the knowledge nets with things like, is like a, tastes like a, smells like a ISA, all of these –A relationships is a, like a, has a and when the AI crowd lost it's funding, and all those funding drives, they kind of looked over and said, "Oh, there's this other stuff, it's kind of cool. Look, there's these relationships like has on and is on, it's real similar, we ought to just move in." And they kind of did and the vocabulary transitioned over. That's interesting, it's also a little unfortunate because inheritance is not ISA. Inheritance, if you look at it with a very jaded eye, inheritance is the declaration of methods and variables in a subscope and it has nothing to do with ISA whatever, and the notion of ISA can be very confounding. Simple example, an integer is a real number and a real number is a complex number. You could draw that in your UML, it'd very simple with all the inheritance there and so forth, but think about trying to compile it. An integer we would hope would be 16 or maybe 64 bits but if it were to derive from the real number, a real number has two integers in it, a mantissa and a characteristics, the exponent and they used, they implied binary points inside them to make these floating-point number. The floating-point number, the real number, derives from complex but the complex has two real numbers in it, the imaginary and the real part. If you were to think about writing that in C++ or in Java, you would write a structure that could not be compiled because it has infinite thought. Makes perfect sense in English makes no sense at all in software.

Esko Luontola