CS100J, Spring 2001
Thurs 4/5
Lecture 20
-------------------------------------------------------------------------------
Announcements:
+ ACSU lunch this Fri -- to join ACSU, see www.acsu.cornell.edu
+ syllabus updated
+ string_methods example on 4/3 updated
-------------------------------------------------------------------------------
Topics:
+ see Savitch (all of Chapter 7)
+ inheritance
+ sub/super class relationship
+ $extends$
+ subclass objects "use" code from superclass
-------------------------------------------------------------------------------
Summary from Lecture 19
+ chars as primitive type
+ Strings as immutable objects
+ sorting
-------------------------------------------------------------------------------
Summary so far:
+ automated problem solving
+ write instructions using syntax and semantics of computer language
+ instructions as statements
+ expressions -> flow control -> methods -> objects -> arrays
+ all along the way removing redundancy/making more sophisticated constructs
+ what's next? removing redundancy in writing classes
-------------------------------------------------------------------------------
Software reuse:
+ removing redundancy in writing classes
+ example:
  - Class Worker (from lecture 12 and project 4, 5) and Class Rower
  - code very similar for 
    state (efficiency, *ability* to do something)
    behavior (*how* to do something)
+ remove redundancy? 2 choices:
  1) copy/paste code!
  2) use inheritance!
-------------------------------------------------------------------------------
Inheritance:
+ reuse written classes with minimal amount of rewriting code
+ Java handles "borrowing" of class fields
-------------------------------------------------------------------------------
IS A relationships and $extends$:
+ process: write a class then write more classes that EXTEND from the first

+ concept: class B is a class A
  - IS A relationship
  - an object is a specialization of a more general category

+ example: A Rower is a Worker.

+ code:
  class A {}
  class B extends A {}
 
  class _______ { }

  class _______ extends { }
  
+ terms:
  B: subclass/derived class/child class (more specific)
  A: superclass/base class/parent class (more general)

+ what goes in `stuff'? 
  - variables, constructors, methods (typical class memebers)
  - we'll develop each to demo on inheritance gives code reuse
-------------------------------------------------------------------------------
Dealing with just $extends$:
+ syntax:
  class _subclassname_ extends _superclassname_ { stuff }
+ $final$ class
  prevent inheritance with $final classname$
+ creating objects from subclasses:
  basic:    Subclass   var = new Subclass(...)
  advanced: Superclass var = new Subclass(...) (see Savitch 478-479)
+ rules: 
  - subclass object has type of the subclass and all ancestors
  - you can assign a subclass object to a superclass variable
    but not vice versa
+ primitive example: $double x = 7$
  - double gets int
  - the "wider" type can accept a more "narrow" type
  - reverse? use a cast: $int y = (int) 7.7$
  - same for inheritance: $Subclass var = (Subclass) new Superclass(...)$
+ terminology:
  - reference type: class type of variable var
  - actual type of object: class type used to create the object
+ see inherit0.java
-------------------------------------------------------------------------------
Inheriting public members:
+ "inherited" member means that a subclass automatically has
  the same field name as the superclass
+ as if you cut and pasted the code from the superclass
+ public instance variables and instance methods 
+ skipping constructors for now -- they're NOT inherited
+ see inherit1.java
+ more syntax rules to come!
-------------------------------------------------------------------------------
Constructors:
+ constructors NEVER inherit (not considered fields!)
+ subclass MUST call constructor of superclass:
  - use $super$(_arguments_) as first line inside constructor 
    (there's one exception to this rule -- see $this()$)
  - must match one constructor of superclass
+ if no constructor written in subclass:
  - default constructor used (Java calls $super()$)
  - $super()$ calls the empty constructor of the superclass
  - if you don't have an empty constructor in the superclass you're in trouble!
+ using $this(arguments)$
  - you may choose to call a constructor of the current class
    with $this(arguments)$
  - the last constructor called MUST call the superclass constructor
+ see inherit2.java
-------------------------------------------------------------------------------
Rules for creating objects:
+ Java does the following when creating an object in this order:
  - allocate memory for the fields of the object (including inherited ones)
  - set ALL fields to default values ("zeros")
  - invoke each constructor in the chain of $this$ constructors (if necessary)
  - invoke the superclass constructor
    (for more ancestors, all constructors are activated all the way "up")
  - all fields are initialized in the order that they're delared
    (top-down, then according to precedence/associativity)
  - the bodies of the constructors are activated
+ see inherit3.java
-------------------------------------------------------------------------------
coming up:
show constructors
show what happens with private var 
shadowing
show static
show what happens with private meth
overriding
packages