CPSC 2150 - DAY 14 OCTOBER 13, 2016 ================================================================================ UNDERSTANDING CLASS RELATIONSHIPS AND A DESIGN PATTERN ------------------------------------------------------ Recall: the implements relation may hold between a class and an interface the extends relation may be between two interfaces or two classes .if B extends A, then B inherits A Abstract classes will factor out common code. Summary of Java-Specific Ideas -If B extends A, then B inherits all the methods of A .Interfaces cannot have constructors .So there is no good place to write seperate contracts for the constructors of classes that implement an interface .Interfaces cannot have static methods .So there is no good place to write seperate contracts .Constructors are not inherited .So in the situation above, the class B must include bodies for any constructors that are expected, even if they would be identical to those of A .The bodies of the constructors in B generally would simply invoke the constructors of A, which is done using super(..) .Static methods are inherited. Implements may be inferred automatically Can only inherit from one parent Declared/Oject Type Rule .The declared type of some variable is the interface type I .The dynamic or object type of that variable is the class type C .Then the relation C implements I must hold Polymorphisim ------------- Java and other object oriented languages decide which method body to use for any call to an instance method based on the dynamic type of the reciever .This type, because it is the class of the constructor, is always a class type .This behavior for calling methods is called polymorphism: "having many forms" Template Method Pattern ----------------------- Recall Pattern: .Base class contains both template and hook methods .Template method calls this.hook method .Hook methods are overridden in derived classes .Template method is not To support this pattern: .Template method is declared final .Either hook methods are in the interface .Or the hook methods are declared abstract .So the base class is abstract, too