CS100J, Spring 2001 Tues 4/10 Lecture 21 ------------------------------------------------------------------------------- Announcements: + P5 due NOW + prelim 3 review up (except specific topics -- Thurs) ------------------------------------------------------------------------------- Topics: + see Savitch (all of Chapter 7) + inheritance + sub/super class relationship + $extends$ + subclass objects "use" code from superclass ------------------------------------------------------------------------------- Topics: + inheriting public methods + overriding public methods + inheriting public variables + shadowing public variables + using $super$ reference + intro to encapsulation&inheritance ------------------------------------------------------------------------------- Summary from Lecture 20: ======================= Inheritance: + software reuse + subclasses inherit (`copy') code from superclasses Reminder of IS-A and $extends$: + subclass can be type of superclasses: - If Y is an X, then $X var = new Y()$ is OK. - Also, Y is a Y, so $Y var = new Y()$ is OK. + called POLYMORPHISM (discussed more later) Reminder of constructors: + subclass must call superclass constructor - constructor of superclass invoked to initialize inherited fields - subclass MUST either call constructor current class or superclass as first statement + order for constructing object: set defaults, invoke constructors from sub to super, initialize variables at uppermost super, execute the constructor of uppermost super, and repeat for subclasses Inheriting public instance variables and methods: + reminder: subclass inherits these automatically + see inherit2.java + some exceptions, though: encapsulation and using the same var/meth in the subclass ------------------------------------------------------------------------------- Methods: + use the actual object to invoke the method, not the ref type! - so, methods accessed from the subclass even if written and and called in the superclass - also called dynamic binding/late binding/dynamic method lookup (Savitch 7.3) + methods use variables written in the SAME class if they're not inherited from another class see inherit4 and inherit5 + may override methods - write method in subclass with IDENTICAL header (same type, params, types, order, amount) - always use the version written in the subclass - why? might need to change behavior in subclass from superclass see inherit6 and inherit6_details + overriding vs overloading: override: same header -- call subclass's version overload: different header -- call current class's version ------------------------------------------------------------------------------- Variables: + access using declared type of reference, not actual object! (from external call) + methods access instance variables from same class (see above) see inherit2, inherit2_vars, inherit3 + shadowing variables: - subclass uses same variable name as super - usually you should avoid doing this! + rem that methods use variables written in same class - methods that are inherited will call vars from the superclass (why? methods use vars from the same class in which the methods are written) - methods that are overriden with use vars in the subclass (why? same reason as above) see inherit7, inherit7a ------------------------------------------------------------------------------- Accessing the superclass variables and methods: + want to bypass overriding and shadowing + use $super$ as a reference + refers to most immediate superclass as if it were an object ($super$ treats the current object as an instance of superclass) + cannot say super.super.... must be crafty with code to get that effect!