Java Composition and Creating User-defined Data Types
In Java, your
Rental class will have attributes that are references to the other object:
View the code below to see an example
private Date startDate, returnDate, actualreturnDate;
private Customer rentedby;
private Video itemrented;
// remainder of class omitted
As you progress in an object-oriented design, you will likely encounter objects in the problem domain that contain other objects.
In this situation you will be drawn to modeling a similar arrangement in the design of your solution.
In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition,
the act of composing a class out of references to other objects. With composition, references to the constituent objects become fields of the containing object.
For example, it might be useful if the coffee cup object of your program could contain coffee.
Coffee itself could be a distinct class, which your program could instantiate.
You would award coffee with a type if it exhibits behavior that is important to your solution.
Perhaps it will swirl one way or another when stirred, keep track of a temperature that changes over time, or keep track of the proportions of coffee and any additives such as cream and sugar.
To use composition in Java, you use instance variables of one object to hold references to other objects. For the CoffeeCup example, you could create a field for coffee within the definition of class CoffeeCup.