ziggy
ziggy

Reputation: 15876

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.

Upvotes: 0

Views: 166

Answers (5)

Anand
Anand

Reputation: 14915

E. The code is loosely coupled.

It's not correct. It is tightly coupled with employee class, for it to be loosely coupled, it must either work on interface or an abstract class. To make it loosely coupled, code would be as following.

interface IEmployee{
...
...
}

class Employee implements IEmployee {
...
...
}

public class MyApp implements Mungeable {
    public void doMunging() { ; }
    //declare interface type
    private IEmployee emp;
    //use DI to inject  an implementation of IEmployee type in MyApp 
    public MyApp(IEmployee object){
    emp = object;
    }
    public static void main(String[] args) {
        emp.setName("bob");
        System.out.print(emp.getName());
    } 
}

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

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

No, e.g.:

class Person{
    // Person has-a name, 
    // where Person is not a String,
    // neither reverse
    private String name; 
}

Upvotes: 1

tobiasbayer
tobiasbayer

Reputation: 10379

For MyApp to have a relationship to Employee, Employee e should be a member variable and not just be instantiated locally in a static method.

Upvotes: 1

alf
alf

Reputation: 8513

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.

Upvotes: 1

Vaandu
Vaandu

Reputation: 4935

MyApp doesn't have Employee, no member is defined. MyApp has main method, thats it. As per below code, MyApp has-a Employee.

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

Upvotes: 4

Related Questions