File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pls explain this OO question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pls explain this OO question" Watch "Pls explain this OO question" New topic
Author

Pls explain this OO question

Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
Which statement(s) are true?
A. Has-a relationships always rely on inheritance
B. Has-a relationships always rely on instance veriables
C. Has-a relationships always require at least two class types
D. Has-a relationships always rely on polymorphism
E. Has-a relationships are always tightly coupled

The answer is B.

Can someone explain me how? I am basically unable to find any connection. Also if possible provide reasoning for the incorrectness of the other amswer chioices.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

>A. Has-a relationships always rely on inheritance
No, Is-a relationship rely on inheritance. For example, Dog Is-A Animal relationship.

>B. Has-a relationships always rely on instance veriables
Yes. For example,
class Person {
String name;
...
}
Person Has-a name

>C. Has-a relationships always require at least two class types
No, only one. It doesn't matter what a class Has.

>D. Has-a relationships always rely on polymorphism
No relation.

>E. Has-a relationships are always tightly coupled
No. A class having a member of a certain interface makes it loosely coupled.


[My Blog]
All roads lead to JavaRanch
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
Originally posted by Satou kurinosuke:
>
>B. Has-a relationships always rely on instance veriables
Yes. For example,
class Person {
String name;
...
}
Person Has-a name


but generally we express HAS-A relationship as a class containing a reference to an obect of same or other class...isn't it? when did instance variables come into picture....i refer K & B book, but I think that this book fails to provide any explanation on this....
HungryJavaGoat
Greenhorn

Joined: Jun 30, 2006
Posts: 23
Hi,
As you said ,HAS-A relationship as a class containing a reference to an obect of same or other class. But..here the word "containing" means that class has a member variable (reference) which refers to an object of same of other class.
If it the reference is used only in some member methods , or method parameter then the relationship becomes "USING". For example, if class A uses reference of class B in its methods then it means that class A is having USING relationship with class B. Pls correct if iam wrong.

Regards,
Karthik
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In the following example (with no instance variables), isn't it true that Foo HAS-A Bar? Isn't this composition?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Good question!

IS-A and HAS-A relationships are generally thought of as class-to-class or object-to-object.

In the above case, a Foo class has a Bar object, which means many Foo objects share a single Bar object. This is a class-to-object relationship, which probably demands a more complex terminology than IS-A and HAS-A.


SCJP 1.4 - 95%
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
i think i got it.....i suppose i got a bit confused
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls explain this OO question