Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Object Orientation

Which Man class properly represents the relationship "Man has a best friend who is a

Dog"?



A. class Man extends Dog { }

B. class Man implements Dog { }

C. class Man { private BestFriend dog; }

D. class Man { private Dog bestFriend; }

E. class Man { private Dog<bestFriend>; }

F. class Man { private BestFriend<dog>; }


Answer to this i found is D

Please Explain Why



it should be


class Man
{
BestFriend b;
}
class BestFriend extends Dog
{}



Isn't it

If we take the wording literally with the use of "is a" as is, your code suits the best. But still, if you have to pick one out of the six given answers, 'D' would be the way to go.


However, if we really look at the actual meaning, the answer D suits better than your code.


class BestFriend extends Dog {
}


When we extend a class, we are really 'extending' it to provide more features. Consider the relationship between 'mammal' and 'cat'. In the real world, a cat actually is a type of mammal, which shares all the characteristics mammals share in common, with some extended features and capabilities. That way a cat ideally shares an is-a relationship with mammal.


Your code as shown above does not accurately define a relationship between the two. If we translate that to the real world subjects, it implies best friends are dogs! Or, all best friends share common characteristics with dogs. That definitely is not what the original statement implies.


On the exam, if you see a real world statement like this, it's better not to take the wording of "is-a" and "has-a" that literally.

ExamLab © 2008 - 2024