cs205: engineering software?
05 April 2010

cs205 Monday 18 September 2006

Upcoming Schedule

Subtyping and Inheritance Notes


B is a subtype of A means wherever an object of type A isexpected, we can use an object of type B instead.
Object is the ultimate supertype of every object type in Java.

Reuse the implementation of the supertype to implement a subtype.

class B extends A means:

class C implements I means: Should a type be permitted to have more than one subtype?

Should a type be permitted to have more than one supertype?

Should a class be permitted to inherit from more than one class?

public class ExtremeLifeCell extends Cell {
   public CellState getNextState () 
       // EFFECTS: Returns the next state for this cell. 
       //    The next state will be alive if this cell or any of its neighbors 
       //     is currently alive. 
ExtremeLifeCell is a subtype of Cell. Anywhere the code expects a Cell object, we can use an ExtremeLifeCell also. For example,
   public class Grid {
     Cell [][] cells;
the elements of the cells array in Grid can be ExtremeLifeCell objects.

The ExtremeLifeCell overrides the getNextState method and inherits the rest of the Cell implementation including its rep (note that we don't need any state to represent an ExtremeLifeCell) and the countAliveNeighbors and getState methods.

Apparent and Actual Types

Apparent types are associated with declarations: they never change
Actual types are associated with object: they are always a subtype of the apparent type

Compiler does type checking using apparent type.
Virtual Machine does method dispatch using actual type.

Can the apparent type of an array element ever change?

How can you change the actual type of a variable?