Distinguish between Classes and Objects. Define a Class
Class Truck extends Vehicle {
private SteeringWheel mySteerer;
}
Here we see that when we create a Truck
Object it has-a SteeringWheel
. A composition relationship. Now, where is the Class definition of the SteeringWheel
? So far you don't know.
It could be in a separate Class file:
Class SteeringWheel {
// stuff
}
Or alternatively we could make it an inner class:
Class Truck extends Vehicle {
private SteeringWheel mySteerer;
private Class SteeringWheel {
// some stuff
}
}
In either case, we still have Composition. The thing that the Inner Class is giving us is a structure in the code, by making it an Inner Class we restrict the scope of the SteeringWheel
class, it's only visible in the Truck
class.
So Inner Classes are primarily a tool for organizing the implementation - good code organization aids maintenance.