Section 6: Overloading, Overriding, Runtime Type and Object Orientation (Chapter 6, Chapter 11)

  • State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a".

  • Write code to invoke overridden or overloaded methods and parental or overloaded constructors and describe the effect of invoking these methods.

  • Write code to construct instances of any concrete class including normal top-level classes and nested classes.

Study Notes

Is-a relationships are implemented through inheritance; has-a relationships are implemented through aggregation. Subclass instances may take on the role of superclass instances, but not vice versa. Encapsulation in object-oriented design places the focus on an object's contract and safely hides its implementation from clients.

Pay attention to which casts are allowed between superclasses and subclasses at compile time, and the effect of casts at runtime. Polymorphism is the result of dynamic method binding of overridden methods at runtime, and the binding is based on the actual type of the object, not the type of the reference. It is illegal for overriding methods to contradict the declaration of the overridden methods. Overloaded methods are distinct methods not subject to dynamic method binding. The ability to overload method names is purely a result of methods being identified by the full signature, rather than just by the method name.

Special language constructs allow explicit access to variables and methods in superclasses and in enclosing contexts. Shadowed variables can be accessed, and overriding methods may call the overridden versions of the methods.

Interface and class definitions can be nested. The exact nature of such definitions and the restrictions placed on the definitions depend on the declaration context. Instances of some nested classes are associated with an outer instance. It is important to understand the correlation between an instance of an inner class and the outer class, and what can be accessed from within an inner class and how. There are several ways of declaring nested classes, and this affects the correlation between an instance of an inner class and its outer class.