Search code examples
javaoopscjp

Do classes have to be on the same inheritance tree for them to have a Has-A relationship


class Employee {
    private String name;
    void setName(String n) { name = n; }
    String getName() { return name; }
}
interface Mungeable {
    void doMunging();
}
public class MyApp implements Mungeable {
    public void doMunging() { ; }
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("bob");
        System.out.print(e.getName());
    } 
}

And the possible answers:

Which are true? (Choose all that apply.)
A. MyApp is-a Employee.
B. MyApp is-a Mungeable.
C. MyApp has-a Employee.
D. MyApp has-a Mungeable.
E. The code is loosely coupled.
F. The Employee class is well encapsulated.

While answering the above question i selected options B,C,E and F

Apparently the only correct answers are B,E and F. For MyApp to have a Has-A relationship with Employee both have to be in the same inheritance tree hierarchy. Is this correct? I thought that if a class has the object as a member it automatically has a Has-A relationship.


Solution

  • For MyApp to have a Has-A relationship with Employee both have to be in the same inheritance tree hierarchy. Is this correct?

    It is not correct.

    I thought that if a class has the object as a member it automatically has a Has-A relationship.

    You were right. Point is, MyApp does not have Employee as a member.