CS 100, Summer 2001 Thursday, 7/26 Lecture 17 ----------------------------------------------------------------------- Announcements: + Extension: Project 1 due 7/27 + Pick up graded Exercise 11 + reading: Bring questions for tomorrow ----------------------------------------------------------------------- Topics: + Inheritance + Sub/super classes + Constructors + Method overriding ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Inheritance - Enables more reuse of code - Tree: subtypes (subclasses): Conifer, Deciduous (draw hierarchy) - Exception: IOException: FileNotFoundException + Terminology - base class: the original class - derived class: the class that "inherits" from or "extends" the base class + How does it work in Java? - EXAMPLE: Inherit1.java + Assignment - You can assign an object of a derived class to a variable of any ancestor class, but not the other way around - EXAMPLE: Inherit2.java + Why is this useful? - What if I want an array of similar, but not identical, things all together? We know that arrays have to be composed of items that are all the same type. Well, Conifers and Deciduous are both Trees, so we can in fact create an array of Trees and store both things inside it. EXAMPLE: Forest.java. - Note the getClass() method we can call to find out what class an object is. + Constructors - use super(): It must come first in your constructor. If you leave it out, Java will call the default constructor for the superclass. - EXAMPLE: See constructors for Conifer and Deciduous + Overriding methods - If subclass has a method with the same name and input args as a method in the superclass, the subclass's method will override the superclass's method - EXAMPLE: toString() is a method in the Object class that we regularly override with our own (nice) output methods - EXAMPLE: water() is overridden by both Conifer and Deciduous - Sometimes you'll want to call both the superclass's method and the overriding version; use super.method() to do this. - EXAMPLE: graphics programs inherit from JFrame and override the paint() method. Remember how we called super.paint()? - EXAMPLE: toString() in Conifer and Deciduous - Overriding is NOT the same as overloading - Note in Forest.java that we call water() on all of the objects in the forest array. Now this is a Tree array, but the method that gets used depends on what object is actually in each entry. So Tree objects use Tree.water(), Conifers use Conifer.water(), etc. This is very cool! + Accessing methods - Conifer and Deciduous each have extra methods that regular Trees do not (getCone(), setSeason()). If we store them all in a Tree array, and we want to access these methods, we need to cast the entries back to whatever type we're interested in. See Forest.java. Note you will NOT be able to cast a Conifer to a Deciduous (or vice versa)! + 'final' prevents inheritance (for classes) + 'final' prevents overriding (for methods) + private methods are not inherited ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP graded Exercise 11, handed out Exercise 12