CS100J, Spring 2001 Thurs 4/12 Lecture 22 ------------------------------------------------------------------------------- Announcements: + P5 almost done (trying to get to Carpenter on Fri) + Prelim 3 Tues, Uris Aud (see web) + review session changed to Mon (see web) + file updates (see Announcements) ------------------------------------------------------------------------------- Topics: + encapsulation and inheritance + $static$, $final$ and inheritance + $Object$ class ------------------------------------------------------------------------------- Summary from Lecture 21: ======================= + methods called from actual object + methods call fields (variables) from class in which method is written + overriding means method with same header called in subclass, not superclass + fields (variables) called from reference variable class + variable shadowing means a variable with same name in superclass is used in subclass ------------------------------------------------------------------------------- Inheritance & Encapsulation: + data hiding with subclasses + reminder on info hiding: - protect class against accidents/deliberate attacks - helps debugging and working with teams ------------------------------------------------------------------------------- $public$: + least restrictive + member is inherited and visible $private$: + most restrictive + prevents subclass from seeing/directly accessing the member (and constructors, too! see inherit9a) + sometimes referred to as "not inherited", but some authors hate that (sometimes also called "not defined") + use inherited code to access others (see Appendix 5 Savitch) + $protected$ - visible within a package - visible to any subclass regardless of package + default ("blank") - visible within a package (collection of classes, Savitch pp 364-367) - not visible to any class OUTSIDE of package, including subclasses see inherit9 ------------------------------------------------------------------------------- How to access "uninherited" members? + can't use $super.member$ see inherit10 + use non-$private$ members to "see into" the "uninherited" members - realistically, all members from super classes indirectly available - otherwise, would cause many, many errors in programming! see inherit11, inherit12, inherit13 ------------------------------------------------------------------------------- What about overriding & shadowing? + variables can be overriden, including different visibility + method visibility cannot be reduced in subclass + in terms of greatest visibility, $public$ > $protected$ > default > $private$ see inherit14 ------------------------------------------------------------------------------- $static$: + inherits, but cannot be overriden or shadowed + $static$ methods and varibles always accessed with reference type see inherit15 ------------------------------------------------------------------------------- $final$: - variables: cannot reassign - classes: cannot extend (create subclasses) - methods: cannot override see inherit16 ------------------------------------------------------------------------------- $Object$: + Every class in Java is a subclass of class $Object$. + don't usually write $extends Object$ in the class header, but you could: $class Blah extends Object$ and $class Blah$ mean the same thing! + $Object$ gives methods like $toString$ and $equals$ - for many of these methods, user must define method body for class - tells specific class how to handle the methods see inherit17 -------------------------------------------------------------------------------