COMPSCI 61B Lecture Notes - Lecture 10: Redundant Code, Double-Ended Queue, Negative Number
Document Summary
Polymorphism: providing a single interface to entities of di erent types . Consider a variable deque of static type (a. k. a. compile-time type) deque: When you call deque. addfirst(), the actual behavior is based on the dynamic type (a. k. a. run-time type) Java automatically selects the right behavior using what is sometimes called dynamic method selection . Suppose we want to write a program that prints a string representation of the larger of two objects. Suppose we want to write a function max() that returns the max of any array, regardless of type. One approach to maximizing a dog array: leave it to the dog class. Disadvantage: speci c to only dogs, will need to do again for cats, horses, etc. Fundamental problem: objects cannot be compared to other objects with > Solution: create an interface that guarantees a comparison method. Write maximizer class in terms of this interface. Negative number if this is less than obj.