aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is has-a transitive? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is has-a transitive?" Watch "Is has-a transitive?" New topic
Author

Is has-a transitive?

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example of the has-a relationship that I had trouble with.

The author says
B is-a A
B has-a C
A has-a C //Is this true?
When I add the following class, compile and run, I get stack overflow.

java.lang.StackOverflowError
at A.<init>(Test18.java:2)
at B.<init>(Test18.java:4)
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi marlene
okay...
-----
B is-a A
B has-a C
A has-a C //Is this true?
------
here v miss one thing- A has-a B which is the reason for A has-a C is true. ie. transitivity is true here.
A->B, B->C => A-> C.
now the second part where u get stack overflow...
the problem is A has B. B extends A. now, when u try to create B's object super constructor A() gets called and the Variable for B object in A class tries to get initialized that is via B(). NOw, again B() calls A's super constructor and so on....its a INFINITE RECURSION!!
thats y u get a Stackoverflow..
regards
maulin
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Marlene,
Originally posted by Marlene Miller:
Here is an example of the has-a relationship that I had trouble with.

The author says
B is-a A
B has-a C
A has-a C //Is this true?

Is has-a transivitive?
IMHO, I would say YES to this question. Aggregation is a whole/part or is-a part of relationship. So a whole can contain parts directly or indirectly.

A car has a wheel
A wheel has a tire
So a car has a tire

So similarly,

A has-a B
B has-a C
A has-a C

But it is not symmetric - a whole can contain its parts, but its part cannot contain its whole.
Just my 2-cents
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Marlene -
I'd say it's a badly written question... (jeez, I hope it wasn't mine :roll: ),
Can you eliminate the recursive aspect from your
example and restate the question?
- Bert
p.s. This could be another classic, fun 'Marlene Special'
[ July 17, 2003: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Maulin, Alton and Bert for taking an interest in my question.
Can you eliminate the recursive aspect from your example and restate the question?

Yes and No.
My second question was whether the has-a relationship is a transitive relationship?
class C { D d = new D(); }
class D { E e = new E(); }
C has-a E?
Thank you Alton for your point of view. I have also discovered that the UML Reference Manual says both generalization and aggregation are transitive. So that question is answered.
The question on the mock exam asks you to select all valid answers.
(a) C is a B
(b) A has a C
(c) B is a A
(d) A is a B
(e) B has a C
When I drew a UML diagram, I noticed the unusual aggregation from A to B. By way of contrast, in the Composite and Decorator patterns, B extends A and B aggregates A.

I could not imagine how to write code to instantiate a B without getting into trouble. Thank you Maulin for your comments.
My first question is: Look at this pathological has-a relationship. Does it confuse you as much as it confuses me?
[ July 17, 2003: Message edited by: Marlene Miller ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Marlene -
Still not sure precisely what your question is?
I can say that the general rule is that it's a bad idea for a superclass to instantiate one of its subclasses. You won't find any examples of that happening on the real exam.
-Bert
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Bert.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is has-a transitive?