Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.

Similar presentations


Presentation on theme: "1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort."— Presentation transcript:

1 1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort

2 Polymorphism The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects at different points in time All object references in Java are potentially polymorphic and can refer to an object of any type compatible with its defined type Compatibility of class types can be based on either Inheritance or Interface

3 Polymorphism Suppose we create the following object reference variable ( Holiday can be a class or an interface): Holiday day; Java allows this reference to point to a Holiday object or to any object of any compatible type If class Christmas extends Holiday or if class Christmas implements Holiday, a Christmas object is a compatible type with a Holiday object and a reference to one can be stored in the reference variable day: day = new Christmas();

4 References and Inheritance An object reference can refer to an object of its class or to an object of any class related to it by inheritance For example, if the Christmas class extends the Holiday class, then a Holiday reference could be used to refer to a Christmas object Holiday day; day = new Christmas(); Holiday Christmas

5 References and Inheritance Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignment The widening conversion is the most useful Assigning an parent object to a child reference can be done, but it is considered a narrowing conversion and two rules/guidelines apply: –A narrowing conversion must be done with a cast –A narrowing conversion should only be used to store an object back to or toward its original class (back to what it was “born as”)

6 Polymorphism via Inheritance It is the type of the object being referenced, not the reference type, that determines which method is invoked If the Holiday class has a celebrate method, and the Christmas class overrides it, consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes the Holiday version of celebrate() If day refers to a Christmas object, it invokes the Christmas version of celebrate()

7 Polymorphism via Inheritance Holiday ChristmasNewYearThanksgiving Holiday day; Christmas xmas = new Christmas(); day = xmas; // widening conversion (no cast needed) NewYear auldLangSyne = new NewYear(); day = auldLangSyne; // widening conversion (no cast needed) Thanksgiving turkey = (Thanksgiving) day; // not back to what it was “born as” // however, compiler accepts cast // will cause a run time exception

8 References and Interfaces An object reference can refer to an object of its class or to an object of any class related to it by an interface For example, if a Christmas class implements Holiday, then a Holiday reference could be used to point to a Christmas object Holiday day; day = new Christmas(); Holiday Christmas

9 Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Speaker current; The current reference can be used to point to any object of any class that implements the Speaker interface The version of speak that the following line invokes depends on the type of object that current is referencing current.speak();

10 Polymorphism via Interfaces Suppose two classes, Philosopher and Dog, both implement the Speaker interface, but each provides a distinct version of the speak method In the following code, the first call to speak invokes the Philosopher method and the second invokes the Dog method: Speaker guest = new Philosopher(); guest.speak(); guest = new Dog(); guest.speak();

11 Summary of Polymorphism public class Christmas extends Holiday implements Observable, Ignorable { // code here } Holiday APIIgnorable APIObservable API Christmas API Object of class Christmas Why only one class name here?

12 Summary of Polymorphism Object instantiated as:Can/Cannot be cast: Child or Later Descendent Class To Parent or Earlier Ancestor (and back to its original class) Parent or Earlier Ancestor Class Any Class To any Interface it implements (and back to its original class) To Child or Later Descendent Any Interface (Not Allowed) To any “incompatible class”

13 Polymorphism: UML Class Diagrams You see how both Inheritance and Interfaces can be used to support polymorphic object references You should now be able to understand why both Inheritance and Interfaces are shown with the same “generalization” arrow icon in UML class diagrams Any Class Parent Class (Extends) Any Class Interface (Implements)

14 Comparator Interface 14 Comparator Interface requires two methods: int compare (T thisOne, T thatOne) boolean equals (Object o) Usually we only care about the compare method Returns an int value that is: Negative when “this” object is less than “that” object Zero when “this” object is equal to “that” object Positive when “this” object is greater than “that” object We can have multiple Comparator classes - one for each useful ordering of the specific object type It is safe to not override the Object class equals

15 Comparator Interface import java.util.Comparator; public class CompareAlpha implements Comparator { public int compare(String thisOne, String thatOne) { return thisOne.compareTo(thatOne); } public static void main(String [ ] args) { CompareAlpha ca = new CompareAlpha(); ca.go(ca); // rest of main is in go } private void go(Comparator c) { System.out.println(c.compare("Hello","World")); // returns -15 }

16 Comparator Interface import java.util.Comparator; public class CompareLength implements Comparator { public int compare(String thisOne, String thatOne) { return thisOne.length() - thatOne.length(); } public static void main(String [] args) { CompareLength cl = new CompareLength(); cl.go(cl); // rest of main is in go } private void go(Comparator c) { System.out.println(c.compare("Hello","World")); // returns 0 (zero) }

17 Comparator Interface Project 2 needs three Comparator classes Comparators will compare “County” objects: import java.util.Comparator; public class PopulationComparator implements Comparator { public int compare(County thisOne, County thatOne) { // calculate and return an integer here }

18 Sorting with Comparator(s) public void actionPerformed (ActionEvent e) {... // Figure out which Comparator to use based on radio buttons MySorts sorter; if ( population.isSelected() ) sorter = new MySorts ( new PopulationComparator() ); else if ( state.isSelected() ) sorter = new MySorts ( new StatePopulationComparator() ); else sorter = new MySorts ( new NameComparator() );

19 Sorting with Comparator(s) // Figure out which sort method to use if ( insertion.isSelected() ) sorter.insertionSort( array ); else sorter.selectionSort( array ); disp.reset( array ); // now show it }

20 Sorting with Comparator(s) public class MySorts { Comparator comp; public MySorts( Comparator comp ) { this.comp = comp; } // Swaps two Type T objects in the array private void swap( T [ ] o, int i, int j ) { T temp = o[i]; o[i] = o[j]; o[j] = temp; }

21 Selection Sort The approach of Selection Sort: –Select a value and put it in its final place in the list –Repeat for all other values In more detail: –Find the smallest value in the list –Switch it with the value in the first position –Find the next smallest value in the list –Switch it with the value in the second position –Repeat until all values are in their proper places

22 Selection Sort An example: original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9 Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

23 Sorting with Comparator(s) public void selectionSort( T [ ] o ) { int min; for (int index = 0; index < o.length-1; index++) { min = index; for (int scan = index+1; scan < o.length; scan++) if ( comp.compare( o[scan], o[min]) < 0 ) min = scan; swap( o, index, min ); }

24 Insertion Sort The approach of Insertion Sort: –Pick any item and insert it into its proper place in a sorted sublist –Repeat until all items have been inserted In more detail: –Consider the first item to be a sorted sublist (of one item) –Insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition –Insert the third item into the sorted sublist (of two items), shifting items as necessary –Repeat until all values are inserted into their proper positions

25 Insertion Sort An example: insert 9: 3 9 6 1 2 insert 6: 3 9 6 1 2 insert 3: 3 6 9 1 2 insert 2: 1 3 6 9 2 finished: 1 2 3 6 9

26 Sorting with Comparator(s) public void insertionSort( T [ ] o ) { for ( int index = 1; index < o.length; index++ ) { T key = o[index]; int position = index; // shift larger values to the right while (position>0 && comp.compare( o[position-1], key ) > 0) { o[position] = o[position-1]; position--; } o[position] = key; }


Download ppt "1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort."

Similar presentations


Ads by Google