aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Has-A relations must be on the same inheritance hiearchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Has-A relations must be on the same inheritance hiearchy" Watch "Has-A relations must be on the same inheritance hiearchy" New topic
Author

Has-A relations must be on the same inheritance hiearchy

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

From the k&B Practice Exams.






Answer (for Objective 5.5):
 B, E, and F are correct statements about the code.
 A and C are incorrect because the MyApp class “uses” the Employee class, but MyApp isn’t
in Employee’s class hierarchy, and MyApp doesn’t “have” an Employee as part of its state.
D is similarly incorrect because MyApp doesn’t “have” a Mungeable as part of its state.


When answering the above question i selected the option that says MyApp has-a Employee.
The book says that this is wrong because Employee and MyApp are not on the same inheritance tree. Is this correct?

I was not aware that the classes are supposed to be on the same inheritance tree for a has-a relationship.

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Can you print the actual question and all possible answers please?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

HAS-A indicates that the class contains some reference at the instance level.
In the example- Employee is a local variable used in public static void main. Its not really part of MyApp class.
And HAS-A doesnt depend on the inheritance hierarchy- because its composition.

Which is the source you are using for this question and solution?


Mohamed Sanaulla | My Blog
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

James Boswell wrote:Can you print the actual question and all possible answers please?


I have updated the question.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Mohamed Sanaulla wrote:HAS-A indicates that the class contains some reference at the instance level.
In the example- Employee is a local variable used in public static void main. Its not really part of MyApp class.
And HAS-A doesnt depend on the inheritance hierarchy- because its composition.

Which is the source you are using for this question and solution?


It was a question from the K&B practice exams. The description given for the answer is

Answer (for Objective 5.5):
 B, E, and F are correct statements about the code.
 A and C are incorrect because the MyApp class “uses” the Employee class, but MyApp isn’t
in Employee’s class hierarchy, and MyApp doesn’t “have” an Employee as part of its state.
D is similarly incorrect because MyApp doesn’t “have” a Mungeable as part of its state.





Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

O. Ziggy wrote: A and C are incorrect because the MyApp class “uses” the Employee class, but MyApp isn’t
in Employee’s class hierarchy, and MyApp doesn’t “have” an Employee as part of its state.


I think you are confused with this statement right?

The statement above is stating the reasons why A and C are incorrect. The- "the MyApp class “uses” the Employee class, but MyApp isn’t in Employee’s class hierarchy" is justification for A not being correct and the other statement is a justification for C not being correct.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Totally agree Mohamed.
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

I am not clear how "E. The code is loosely coupled." is a correct answer.


MooN
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Has-A relations must be on the same inheritance hiearchy
 
Similar Threads
Question 26 Exam 1 - OCP Java SE 6 Programmer Practice Exams
Sorting arraylist on the basis of object
Confusing Encapsulation and validation question
Polymorphism, arguments and instanceof questions
Problem with @Column annotation or corresponding xml tag if used with @Id annotation