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 relation 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 relation" Watch "HAS-A relation" New topic

HAS-A relation

Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Which statements concerning the relationships between the following classes are true?
class Foo {
int num;
Baz comp = new Baz();
class Bar {
boolean flag;
class Baz extends Foo {
Bar thing = new Bar();
double limit;
1) A Bar is a Baz.
2) A Foo has a Bar.
3) A Baz is a Foo.
4) A Foo is a Baz.
5) A Baz has a Bar.
The correct answers mentioned are 3,5.
My answers are 2,3,5.
Can any one explain why answer 2 is wrong?
Larry Jones

Joined: Oct 22, 2002
Posts: 24
Foo dosen't have-a Bar.
In the code below, Foo has a Bar:

Baz, which extends Foo, has a Bar. But in OO terms, Foo does not have-a Bar.
For a has-a relationship, either a class, or a superclass must have a member that is a reference to the object.
[ March 13, 2003: Message edited by: Larry Jones ]
I agree. Here's the link:
subject: HAS-A relation
It's not a secret anymore!