Skip to main content
Logo image

Section C.17 J2: Immutable Objects, Interfaces, Subtyping

Synopsis.

  • Start with a discussion of immutable vs mutable object.
    • Immutability allows objects to be used like values
    • Effects of shared ownership through aliasing are hard to control.
    • Work through immutable objects with the Vec2 example.
    • Discuss factory methods in light of this example: Need a way to make a Vec2 by cartesian and by polar coordinates. Same arguments, cannot overload the constructor. Static factory method is nice because we can also give it a proper name.
  • Motivate inheritance with Vec2: Why only cartesian or polar.
  • We want to have both. But typing forces us to be specific when using a Vec2, doesn't it? We don't want to be specific with respect to the concrete type, but just say, we expect something that is a Vec2.
  • Java has interfaces to specify that. They introduce a subtyping relation.
  • Introduce Vec2 interface and implement classes PolarVec2 and CartesianVec2. Demonstrate how the type system enforces that all methods required by the interface are implemented in the classes. Show how the Rectangle class can “retreat” to Vec2 and use Polar and Cartesian Vec2's interchangeably.
  • Introduce dynamic dispatch. There are two types: The static type of a reference and the dynamic type. The dynamic type is a property of a program execution: It is the type the object was constructed from which is evident at the constructor call. The static type is an upper bound to each dynamic type: “If the static type is T, in every execution, the referenced object is a subtype of T”. The method to call is determined by the dynamic type and selected at the run time of the program. The Java type system ensures that this method exists.

Sections Covered.