Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
About Exam
easy

Object Composition in Java

Mark
< Prev
Next >

Consider the Employee and Office classes shown below. Which object-oriented design principle is best demonstrated by the relationship between the Office and Employee classes?

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

Composition, since Office contains an Employee object as a manager.

B

Polymorphism, because Office can handle multiple types of Employee objects uniformly.

C

Encapsulation, due to hiding the internal state of the Employee.

D

Inheritance, as Office extends the functionalities of Employee.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|