CS 100, Summer 2001 Wednesday, 8/1 Lecture 20 ----------------------------------------------------------------------- Announcements: + Project 2 due 8/2 + Pick up graded Exercise 13 + Turn in Exercise 14 + Final exam 8/7, 8 a.m. + reading: Savitch 10.2 ----------------------------------------------------------------------- Topics: + Project 2 + Object class + Dynamic binding + Polymorphism ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Project 2 - Selection Sort and Bubble Sort algorithms - Sorting into a local copy: use "null" to mark "used" entries in the input array. - Must increment comparisons whether or not an "if" statement succeeds. (Must count every call!) - MUST make a local copy of the input Card array, for the BridgeHand constructor. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + The Object class - Every class in Java inherits from Object, even classes you write - "extends Object" is implied + Useful things inherited from Object - equals(): returns a boolean indicating whether the two objects are equivalent - toString(): returns a String representation of the object - getClass(): returns the class of the given object - clone(): makes a copy of the object (if the class implements Cloneable) - EXAMPLE: Builtin.java ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Dynamic binding - Java figures out which method to use at runtime (if there is more than one choice, due to overriding) + Polymorphism ("many forms") - Definition: using different definitions of the same method name in different contexts - We've already seen this with the Forest class (an array of Trees, each of which might be a Tree or a Conifer or a Deciduous) - What's on the left side of the = sign dictates which methods/ variables you can access (w.r.t. the object) - What's on the right side of the = sign dictates which version of the variables/methods you'll get EXAMPLES: Tree t1 = new Tree(); // water() is the generic one Tree t2 = new Conifer(); // water() is the Conifer one // (dynamic binding!) Conifer c1 = new Conifer(); // now have access to getCone() Tree t3 = (Tree)c1; // Still uses Conifer's water() // (dynamic binding!) Conifer c2 = new Tree(); // not legal; no getCone() in Tree! Others you can think of? Mystery solved: If you were wondering why System.out.println(t1); works at all (after all, a Tree certainly is not a String or an int or a double or anything else), this is why: there is an overloaded version of println that takes in an Object: public void println(Object obj) { System.out.println(obj.toString()); } Earlier we just said that "Java automatically calls toString for you when you print out an object" - now it should make sense how Java does this! Note that the inner call to println is not an infinite loop, because it is passing a String, and println(String s) is a different method. But wait: String is a class in Java, so it must also inherit from the Object class (like everything else). And that would mean that calling println(s), for a String variable s, could legitimately refer to println(Object obj), because a String "is a" Object. If there were no println(String s) method, this is in fact what would happen (an infinite loop!) because Java would match the inner call with the same (outer) method. But Java has two methods to choose from: println(Object obj) and println(String s). Because the latter is more specific, it gets chosen by Java. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Inheritance design! EXAMPLE: Banana.java and Strawberry.java - Rewrite to inherit from a general Fruit class to enable code reuse - Draw the inheritance hierarchy ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP graded Exercise 13