Ch 23 Subtyping Extension and Inheritance PDF

Title Ch 23 Subtyping Extension and Inheritance
Course Programming Languages and Techniques I
Institution University of Pennsylvania
Pages 4
File Size 114 KB
File Type PDF
Total Downloads 95
Total Views 124

Summary

Subtyping Extension & Inheritance...


Description

Ch 23: Subtyping, Extension, and Inheritance Java class provides template for creating new objects class is also a TYPE - class describes ways in which its instances can be manipulated any instance of class c can be stored in a variable of type c each class is a subtype of any interfaces it implements Subtyping interfaces are also types, so we can declare variables, method arguments, and return values whose types are given by interfaces Displaceable d; d = new Point(1, 2); d.move(-1, 1);

every Point object satisfies Displaceable contract, d holds Displaceable objects and all Point objects are Displaceable, so d can store a Point however, cannot use ColorPoint's methods on d object Multiple Interfaces class may implement more than one interface interface offers POV abt the objects, there may be more than one POV abt the objects in a class public class Circle implements Displaceable, Area public class Rectangle implements Displaceable, Area Circle c = new Circle (10, 10, 5); Displaceable d = c; Area a = c; Rectangle r = new Rectangle (10, 10, 5, 20);

d = r; a = r; // not OK Displaceable d = c; Circle c2 = d; Circle C = new Circle (10, 10, 5); Rectangle r = c;

implements keyword shown as solid line extends keyword shown as dotted Interface Extension public interface Shape extends Displaceable, Area { public Rectangle getBoundingBox(); }

Shape interface is subtype of both the Displaceable and Area interfaces, thus any class that implements Shape must supply all of their methods plus getBoundingBox() method required by Shape Inheritance classes can extend one another but class may only extend ONE class class D { private int x; private int y; public int addBoth() { return x + y;} } class C extends D { private int z; public int addThree() { return (addBoth() + z); } }

subclass C gets its own copy of all the fields and methods of superclass that it extends C has 3 private fields (x, y, and z) and 2 public methods (addBoth and addThree)

even though C extends D, x and y are private so they cannot be mentioned within c's methods protected keyword designates a field or method as visible within the class and all subclasses all subclasses would have to preserve invariant Constructors and super not possible to inherit constructor from the superclass constructor must have same name as the class and superclass must have different name than class that extends it class D { private int x; private int y; public D (int initX, int initY) { x = initX; y = initY; } public int addBoth() { return x + y;} } class C extends D { private int z; public C (int initX, int initY, int initZ) { super(initX, initY); z = initZ; } public int addThree() { return (addBoth() + z); } }

call to super must be first thing done in constructor body Object root of the class hierarchy is Object all reference types in Java are subtype of Object toString, equals methods

Static Types vs Dynamic Classes difference is whether info is determined at compile time or whether it isn't available until the program is run...


Similar Free PDFs