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 Object Orientation 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 "Object Orientation" Watch "Object Orientation" New topic
Author

Object Orientation

aruna dabas
Greenhorn

Joined: Dec 16, 2008
Posts: 27


class One {
private int x;
int getX() { return x; }
}

class Two {
private int y;
public void setY(One o) {
y = o.getX();
}
}

Which is true?

These classes are NOT coupled.
These classes are loosely coupled.
These classes are tightly coupled.
These classes are abstractly coupled.


Q:Which is true?

Has-a relationships must be tightly coupled.
Is-a relationships must be well encapsulated.
Is-a relationships can be represented by using reference variables as local variables.
Has-a relationships can be represented by using reference variables as local variables.
Has-a relationships can be represented by using reference variables as instance variables.

Please Explain me the answers as i am confused in this kind of questions.
Thanking You
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
For the first example, I think the classes are loosely coupled. That's because the second class doesn't rely on the implementation of the first class. The programmer/s of the first class can change the implementation of getX() (for example, in order to return a random integer within a range) without breaking the code of the second class.

For the second example:
Has-a relationships must be tightly coupled.
(False, as long as you use the API of the classes for the objects you have.)
Is-a relationships must be well encapsulated.
(False, the question doesn't make any sense.)
Is-a relationships can be represented by using reference variables as local variables.
(False, Is-A is about inheritance.)
Has-a relationships can be represented by using reference variables as local variables.
(False, local variables don't have anything to do with it. Local variables live inside methods.)
Has-a relationships can be represented by using reference variables as instance variables.
(True, if class A Has-A B this means that class A has a reference of type B.)


All code in my posts, unless a source is explicitly mentioned, is my own.
aruna dabas
Greenhorn

Joined: Dec 16, 2008
Posts: 27
Thanks Ruben.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Whilst Reuben is right, I think, you should seek more information yourself. The Ranch encourages posters not to answer questions outright but to aid in seeking further information. Giving someone the answer does not truly help to understand an issue outright.



be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I thought I was explaining in enough detail the rationale to my answers, and it's not the first time coupling has come up in the forum either. Is there anything else that either Aruna or Stephen (or anyone else in the forum for that matter) would like to discuss about it? Also, how do you think I could have phrased my answers better?
 
 
subject: Object Orientation