• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is has-a transitive?

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic