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

I just read the book and it had the terms "HAS-A" and "IS-A". Does anyone know what they mean specifically? I tried searching the book, but the book lasted 600 pages. Thank!

+47
oop
Feb 08 2018-10-10T00
source share
10 answers

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);
  • The house has a room (composition);
  • There is a passenger in the house (aggregation).

The difference between (2) and (3) is subtle, but important for differentiation. Together they are forms of association. What's the difference? Composition implies that the child cannot live outside the context of the parent (destroys the house and the room disappears), while aggregation implies that the child can exist on its own (destroy the house and the resident in another place).

+115
Feb 08 2018-10-10T00
source share
— -

A Car has-a Wheel .

A Sparrow is-a Bird .

Academically, terms are used to choose between composition and inheritance.

+48
Feb 08 2018-10-10T00
source share

Has-a means that the class in question has a type field.

Is-a means that the class is distributed from a superclass or implements an interface. The instanceof operator would return true if it had been tested against the class in this case.

+14
Feb 08 2018-10-10T00
source share

This is not a java specification, it is specific to OO.

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

Sometimes there is no IS-A relationship between two classes, but "HAS-A" is more appropriate. For example, a chair "HAS-A". or several. this is called aggregation, not inheritance.

I will not understand in detail when to use them, because it depends on how the classes are used, and even if it is known that there is so much to consider, a new question for a particular case would be more appropriate.

+6
Feb 08 '10 at 0:20
source share

It is a special kind, for example. A car is a special type of vehicle.

Has a = Physically something has, for example, a car has an engine.

+4
Feb 08 2018-10-10T00
source share

IS-A, HAS-A, etc. not really very OO. Instead, the 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 K. Martin: The word "ISA" penetrated our vocabulary and, incidentally, one word ISA, it penetrated our vocabulary through a circular route and became very important in object-oriented circles, but it did not start like that. He crept in 80 through the crowd of AI, which created these wonderful networks of knowledge, you could remember this, all the hype about artificial intelligence in the late 80s, early 90s, and then created these structures that will go on the network knowledge, and the relationship between entities and knowledge networks with things like a, tastes like a, smells like ISA, all these relationships are like a, when and when the AI ​​crowd lost funding, and all those who finance the drives , they kind of looked around and said: “Oh, this is something else, it's cool. Listen, there’s such a relationship, ak there, going, they really look like, we just have to move. " And they kind of like the dictionary went over. This is interesting, it is also a little sad, because inheritance is not ISA. Inheritance, if you look at it with a very shaded eye, inheritance is the declaration of methods and variables in the sub-series, and this has nothing to do with ISA, and the concept of ISA can be very confusing. A simple example: an integer is a real number, and a real number is a complex number. You could draw this in your UML, it would be very simple with all the inheritance there and so on, but think about trying to compile it. The integer that we would like to hope for would be 16 or maybe 64 bits, but if it were obtained from a real number, then the real number has two integers in it, the mantissa and characteristics, the indicator and they used, they meant binary dots inside them to make this a floating point number. A floating point number, a real number, comes from the complex, but the complex has two real numbers: imaginary and real. If you were thinking of writing this in C ++ or in Java, you should write a structure that cannot be compiled because it has endless thoughts. The perfect meaning in English does not make any sense in software.

+3
Feb 08 '10 at 0:50
source share

as cletus points out, is-a is different. but be careful with has-a. it can mean composition (responsibility for life), aggregation (part of something) or just uses -a (has a link, knows how to build or find it). the latter is just an association.

+2
Feb 08 2018-10-10T00
source share

" IS A ": Establishes a relationship between related objects. You can use inheritance to establish a relationship.

"HAS A" . Defines the possibility for possibly unrelated objects. You can use the interface to determine the opportunity.

Example:

A dog is an animal. He has the opportunity to find out.

Man is not an animal. He has the opportunity to learn, think, rise and apply.

+1
Sep 15 '16 at 17:04
source share

Names pretty much mean everything.

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

If there is a class named Car . Then any type of vehicle can inherit the attributes of this superclass. For example, a police car inherits all the attributes of a vehicle because it is a specialization of the latter.

enter image description here

The HAS-A class, on the other hand, has a reference to another class or instance of another class. In other words, he shares a connection with another class. There are two types of HAS-A classes, the totality of which means that a class can exist independently and composition, that is, a class can exist only next to the one with which it has a common connection.

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

Taking the Car class, the Driver class will be considered aggregation, because the Driver class can exist as an independent object, even if the Car class ceases to exist.

While the class Engine cannot exist if the class Car does not exist, as the Engine cannot exist outside the scope of the Car .

enter image description here

+1
Feb 16 '17 at 3:50
source share

If you create a collection class called School. You would have “teachers” and “students” at school. You are developing the class "man", and both "students" and "teachers" are inherited from the class "Man", because both are "man." Each student and teacher has a birthday. You must create a “date class” because the teacher and student “have” or “have” a birthday inside the “Person class”. This is a “container”. In short, “a person” uses inheritance “has” uses container ships

0
Oct 21 '16 at 21:41
source share



All Articles