79

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.

1
  • "Is-a" it's reuse of code by inheritance, "has-a" - by delegation. Sep 28, 2017 at 19:48

10 Answers 10

166

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).

4
  • 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
    Feb 8, 2010 at 2:00
  • 4
    4. A House uses an internet provider (association).
    – Bob Stein
    Aug 2, 2015 at 16:20
  • A Point has an x & y coordinate. That's a has-a relation that is composition in your terms. But now if I change the implementation of the Point class to be based on r,theta then the x/y still exist as methods but now they involve computation rather than simply returning a data member. Is there a generalized concept for this sort of access? Aug 25, 2019 at 16:57
  • 1
    This is not right. Composition DOES NOT imply dependence. A car has an engine, but if the car gets wrecked you can pluck out the engine and use it somewhere else. Happens all the time. May 30, 2022 at 20:20
62

A Car has-a Wheel.

A Sparrow is-a Bird.

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

2
  • Do they have to have a real is-a relationship? I mean I have a Doors class which extends Home class, but a Door is not a Home practically, but it extends the Home class programmatically. Apr 26, 2014 at 22:55
  • 4
    @BurakKarakuş: Then it may be wrong. Isa/Hasa is a question you ask yourself to decide whether inheritance is the right answer. Maybe your Door should have a Home, not be a Home.
    – david.pfx
    Apr 28, 2014 at 13:20
19

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.

0
17

The names pretty much imply all.

An IS-A class can be thought of as specialized reference to an instance of another class that inherits all its attributes.

If a class called Vehicle exists. Then any type of vehicle may inherit the attributes of this super-class. For example Police Car will inherit all the attributes of Vehicle because it is a specialization of the latter.

enter image description here

On the other hand, an HAS-A class has a reference to another class or an instance of another class. In other words, it shares an association with another class. There are two types of HAS-A classes, aggregation meaning the class can exist independently, and composition, meaning the class can only exist along side the one it shares an association with.

It is important to know when to classify an object as a composition class or simply as an attribute of another class.

Taking the Vehicle Class again, a Driver class would be considered an aggregation because the Driver class can exist as an independent entity even if the Vehicle class ceases to exist.

Whereas an Engine class cannot exist if a Vehicle class is non-existent, because the Engine cannot exist outside the scope of the Vehicle.

enter image description here

10

"IS A" : Establishes relation between related objects. You can use inheritance to establish the relation.

"HAS A": Defines a capability for possibly unrelated objects. You can use interface to define the capability.

Example:

Dog IS A Animal. It HAS A capability to Learn.

Man IS Not A Animal. He HAS A capability to Learn,Think,Climb and Apply.

8

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.

1
  • 1
    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
    Feb 8, 2010 at 8:35
8

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.

6

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.

1
  • 1
    For mutable entities, 'uses-a' is probably the most important relationship, and the one that gets the least thought.
    – kyoryu
    Feb 8, 2010 at 8:35
5

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.

3

If you develop a Collection Class called "School". You would have "teachers" and "students" in the school. You develop a "person" class and both "student" and "teacher" would inherit from the "Person" class since both are --"is a" person. Both the Student and Teacher each have a birthday. You would create a "Date Class" since both the teacher and student "have" or "has a" birthday inside the "Person Class". This is "containership" In summary "is a person" uses inheritance "has a" uses containership

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.