Object Composition in Java
In an enterprise management system, what design principle does the relationship between the ‘Office’ and ‘Employee’ classes demonstrate?
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public void work() {
System.out.println(name + " is working.");
}
}
public class Office {
private Employee manager;
public Office(Employee manager) {
this.manager = manager;
}
public void startDay() {
manager.work();
}
}
A
Inheritance, as Office extends the functionalities of Employee.
B
Composition, since Office contains an Employee object as a manager.
C
Encapsulation, due to hiding the internal state of the Employee.
D
Polymorphism, because Office can handle multiple types of Employee objects uniformly.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE