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!
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.
A Car
has-a Wheel
.
A Sparrow
is-a Bird
.
Academically, the terms are used to decide between composition and inheritance.
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.
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.
This is object-oriented programming and UML terminology, not Java-specific. There are actually three cases you should be aware of:
- A House is a Building (inheritance);
- A House has a Room (composition);
- 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).
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.
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.