HAS-A, IS-A terminology in object oriented language

Oop

Oop Problem Overview


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.

Oop Solutions


Solution 1 - Oop

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

Solution 2 - Oop

A Car has-a Wheel.

A Sparrow is-a Bird.

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

Solution 3 - Oop

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.

Solution 4 - Oop

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

Solution 5 - Oop

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

Solution 6 - Oop

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.

Solution 7 - Oop

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.

Solution 8 - Oop

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.

Solution 9 - Oop

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.

Solution 10 - Oop

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremyView Question on Stackoverflow
Solution 1 - OopcletusView Answer on Stackoverflow
Solution 2 - OopAnon.View Answer on Stackoverflow
Solution 3 - OopakfView Answer on Stackoverflow
Solution 4 - OopbuydadipView Answer on Stackoverflow
Solution 5 - OopRavindra babuView Answer on Stackoverflow
Solution 6 - OopChris HView Answer on Stackoverflow
Solution 7 - OopfastcodejavaView Answer on Stackoverflow
Solution 8 - OopEsko LuontolaView Answer on Stackoverflow
Solution 9 - OopRay TayekView Answer on Stackoverflow
Solution 10 - Oopuser7055313View Answer on Stackoverflow