Is it possible to create a class that holds another class as data?
Yes, absolutely! For example, we are defining a Pizza
class, but we might also want to make a DeliveryTruck
class that stores a list of pizzas that it is carrying. This sort of classes-inside-classes happens all the time, and we've seen it before with graphics. An object of the Rectangle
class had internal data that included two Point
objects, for example.
Are there things other than getters and setters?
Sure. There can be more complicated functions that change the object in complicated ways. (Think of our withdraw
function for the class BankAccount
--- we didn't just change the internal data, we first required an if
statement to decide what to do.)
One sort of method that we'll write a lot is the .toString()
method, which summarizes the internal information of the object and returns it as a string. This will be useful for debugging purposes. It's more than just a "getter" --- it actually reports many of the object's internal information pieces.