A friendly place for programming greenhorns!
Is has-a transitive?
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)
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
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
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 ]
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 ]
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
Thank you Bert.
today's feeble attempt to support the empire
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA

This thread has been viewed 661 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 20, 2018 13:26:37.