Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.

Similar presentations


Presentation on theme: "Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9."— Presentation transcript:

1 Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9

2 Aalborg Media Lab 15-Jul-152 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 Holiday class is used to derive a child class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday day; day = new Christmas(); Holiday Christmas

3 Aalborg Media Lab 15-Jul-153 References and Inheritance Assigning a predecessor object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an ancestor object to a predecessor reference can be done also, but it is considered to be a narrowing conversion and must be done with a cast The widening conversion is the most useful

4 Aalborg Media Lab 15-Jul-15 References and Inheritance An Object reference can be used to refer to any object –Any object can be referred as an Object through an assignment Double number = new Double(5) Object myObject = number;

5 Aalborg Media Lab 15-Jul-15 Polymorphism A reference can be polymorphic, which can be defined as "having many forms" obj.doIt(); This line of code might execute different methods at different times if the object that obj points to changes Polymorphic references are resolved at run time; this is called dynamic binding

6 Aalborg Media Lab 15-Jul-15 Polymorphism Careful use of polymorphic references can lead to elegant, robust software designs Polymorphism can be accomplished using inheritance or using interfaces

7 Aalborg Media Lab 15-Jul-15 Polymorphism via Inheritance It is the type of the object being referenced, not the reference type, that determines which method is invoked Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it Now consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes the Holiday version of celebrate ; if it refers to a Christmas object, it invokes the Christmas version

8 Aalborg Media Lab 15-Jul-15 Polymorphism via Interfaces An interface name can be used as the type of an object reference variable The interface reference can refer to any object of any class that implements the interface public interface Speaker { public void speak(); public void announce (String str); } Speaker current; current can reference to any object implementing Speaker

9 Aalborg Media Lab 15-Jul-15 Polymorphism via Interfaces We are now able to create polymorphic references among objects implementing the same interface Speaker current; current = new Philosopher(); current.speak(); current = new Dog(); current.speak(); Interface references can’t be used to invoke class methods. current.getName(); // compile error

10 Aalborg Media Lab 15-Jul-15 Polymorphism via Interfaces Use a explicit cast to access object data/methods (Dog(current)).getName(); A parameter to a method can be polymorphic, giving the method flexible control of its arguments public void sayIt (Speaker current) { current.speak(); }

11 Aalborg Media Lab 15-Jul-15 Difference? Advantages? What is the difference / between interface polymorphism and inheritance polymorphism. When shall we use what? Advantages?

12 Aalborg Media Lab 15-Jul-15 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –sorting a list of test scores in ascending numeric order –sorting a list of people alphabetically by last name

13 Aalborg Media Lab 15-Jul-15 Sorting There are many algorithms for sorting a list of items These algorithms vary in efficiency We will examine two specific algorithms: –Selection Sort –Insertion Sort

14 Aalborg Media Lab 15-Jul-15 Selection Sort The approach of Selection Sort : –select a value and put it in its final place into the list –repeat for all other values In more detail: –find the smallest value in the list and switch it with the value in the first position –find the next smallest value in the list and switch it –repeat until all values are in their proper places

15 Aalborg Media Lab 15-Jul-15 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

16 Aalborg Media Lab 15-Jul-15 Swapping Swapping is the process of exchanging two values Swapping requires three assignment statements temp = first; first = second; second = temp;

17 Aalborg Media Lab 15-Jul-15 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

18 Aalborg Media Lab 15-Jul-15 Insertion Sort 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

19 Aalborg Media Lab 15-Jul-15 Insertion Sort An example: original: 3 9 6 1 2 insert 9: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9

20 Aalborg Media Lab 15-Jul-15 Sorting Objects Integers have an inherent order, but the ordering criteria of a collection of objects must be defined Recall that a Java interface can be used as a type name and guarantees that a particular class implements particular methods We can use the Comparable interface and the compareTo() method to develop a generic sort for a set of objects

21 Aalborg Media Lab 15-Jul-15 Comparing Sorts Both Selection and Insertion sorts are similar in efficiency They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Approximately n 2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of order n 2 Other sorts are more efficient: order n log 2 n

22 Aalborg Media Lab 15-Jul-15 Searching Searching is the progress of finding a designated target element within a groups of items (search-pool) Of what nature is the search pool? –are the item sorted? –are the items comparable? Look at two searching algorithms –linear search –binary search

23 Aalborg Media Lab 15-Jul-15 Linear Search The approach of linear search: –Search until elment is found In more detail: –Compare every element, starting with the first element –If search pool contains N elements the target-element is found after max N comparisons. Complexity = N –Statistically the target-element is found after N/2 comparisons

24 Aalborg Media Lab 15-Jul-15 Binary Search The detailed approach of the binary search: –The test-pool must be ordered! –Downsize the test-pool by only comparing the middle element (is the target-element to the left or right from middle element) –Repeat until test-pool is of size one and corresponds to the target-element –Has a complexity of log 2 N

25 Aalborg Media Lab 15-Jul-15 Binary Search 23581015 0123456 mid highlow target

26 Aalborg Media Lab 15-Jul-15 23581015 0123456 mid highlow target Binary Search

27 Aalborg Media Lab 15-Jul-15 Binary Search 23581015 0123456 high mid low target

28 Aalborg Media Lab 15-Jul-15 Exercises –9.1 Programming Projects –9.1 & 9.2


Download ppt "Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9."

Similar presentations


Ads by Google