1

Assume I have the following code.

class D { static Integer i1 = 42; }

Is it true that D has an Integer? Or is it only for instance variable that we can have a has-a relation?

I also wonder about very similar, if a primitive variable also can make a has-a relation or is it strictly for classes? e.g.

class D { int i1 = 42; }

It seems that D has an int but it is a primitive and I'm not sure that primitives are legal for a has-a relationship.

To construct an example that might be awkward, I assume that it is not a has-a relation that the String s1 = "s1"; has a char because it is not that kind of relation even though s and 1 are variables types of char in this case.

Similarly, I assume it's false that a class or an object "has" something that it only has conceptually or as internal implementation rather than the plain way of how we understand a has-a relation.

AFAIK, Java has no class Digit, it has no class Mantissa, it has no class Coefficient and no class Fraction when I browse the javadocs for Java 8, while Java has many classes for many different purposes. I suppose the reason for omitting certain classes that are fairly common concepts could be that the object would not be useful or that the class hierarchy would become unnecessary complex.

Did I understand the usage and definition of the has-a relation? Did I misunderstand something? Are there corner cases that I didn't think of?

  • 1
    You seem to be confusing the class with objects of that class. In your first example, the class D has an Integer. In your second example, objects of type D have an int. The confusion may be because there is an implicit reference to the class D in objects of type D, which allows you to examine static members – Caleth Jan 11 '17 at 10:21
  • Has-a and is-a are OOP design guides. Use them as leads and/or checks when writing classes. Application outside an OOP design context may feel weird because it serves no purpose. – Martin Maat Jan 11 '17 at 18:47
4

Typically, when you are introduced to the terms of is-a and has-a, you mostly learn them as opposed to one antoher is-ahas-a ( I borrowed the notation from linguists ).

Both terms are used like the everyday-language equivalents:

e.g. »a dog is an animal«

which could be translated to Java-lingo:

»The class dog extends the class animal«

The point is here, that you have two classes which have a relationship in some way - of one extending the other via is-a-relationship.

If you know and understand this one, the other one will be as easy.

Talking about the has-a-relationship is a relationship between two classes of being used and being accessible in a very private way:

»dogs have eyes« is the way of expressing, that eyes are a part of dogs and are (in this case) only accessible by this ("instance" of a) dog.

In Java-lingo that means:

»The class dog has (private) members, of which one of them are eyes«. (Nobody would say dogs are eyes to make the contrast clear).

If you look for the signalword extends, you have a strong indication, whether you are looking at a is-a or a has-a relationship.

This is not difficult to know and also not ambigous.


What about your examples?

1) i1 would be a special kind of relationship. In principle, it is a has-a relationship. But, what makes it special is, that i1 is shared between instances. It would be right to claim, that every instance of D has access to the same shared i1.

opposed to

2) Every instance of D has it's own i1.

(1) is the communist version of i1 (vis comica).

  • 1
    I think you should use the word class instead of object. The word object has a tendency to muddle the waters (though it shouldn't). Nice answer. – Bent Jan 11 '17 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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