File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HAS-A concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HAS-A concept" Watch "HAS-A concept" New topic

HAS-A concept

David Fitzmaurice

Joined: Jul 28, 2004
Posts: 3

It’s Wednesday, and I am taking the test on Friday.
But although I have learn many of the rules of Java, there are a few things I still don’t understand.

My question concerns the HAS-A concept, which is one of Sun’s exam objectives.

The expression HAS-A seem misleading since one object never contains another object. All objects live on the heap.
While class definitions can be nested, objects are never actually nested.

It seems to me that to own a reference to an object is just like having it’s phone number.
If object A has a reference to an object B, it can call it up and ask it questions, and give instructions.

And if A passes it’s own reference to B, B can call back, when it likes.
But B is never inside A , so why say that A HAS-A B?

The HAS-A concept seem to me to be misleading: or am I missing something?

Can any of you Cowboys advise ?


Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It is quite true that all objects live on the heap and no object is ever really "contained within" another object. However, when we say that one object "has a" instance of another object, we mean that the first object has a reference to the second object. Basically, the second object is a "part" of the first. Let me try explaining with an example.

Let's say that we have this class, My2DPoint:

Now, let's define a new class, MySquare, which "Has a" My2DPoint (in fact, it has two).

In this case, the class MySquare "Has A" My2DPoint in it. Granted, the actual object isn't contained within the instance of MySquare (they're both on the heap), but the My2DPoint object is a part of the MySquare object.

If you say it in a sentence, it seems to make sense:

"A MySquare object HAS A topLeft My2DPoint object and a bottomRight My2DPoint object."

I hope that helps,
[ July 29, 2004: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955


i think you have a typo - your constructor should be

it's a 2d point, not a 3d point
[ July 29, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by fred rosenberger:

i think you have a typo - your constructor should be

it's a 2d point, not a 3d point

Thanks - I fixed it. Originally, I was making a 3DPoint and a Cube, but for simplicity sake, I decided to go with a 2DPoint and a Square. Apparently, not all of the code got updated. Thanks.
I agree. Here's the link:
subject: HAS-A concept
It's not a secret anymore!