View
215
Download
1
Tags:
Embed Size (px)
Citation preview
Aalborg Media LabApr 19, 2023
Polymorphism
Lecture 12
Chapter 9
Aalborg Media LabApr 19, 2023 2
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
Aalborg Media LabApr 19, 2023 3
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
Aalborg Media LabApr 19, 2023
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;
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
Polymorphism• Careful use of polymorphic references can lead to
elegant, robust software designs
• Polymorphism can be accomplished using inheritance or using interfaces
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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();}
Aalborg Media LabApr 19, 2023
Difference? Advantages?
• What is the difference / between interface polymorphism and inheritance polymorphism.
• When shall we use what?
• Advantages?
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
Swapping
• Swapping is the process of exchanging two values
• Swapping requires three assignment statements
temp = first;
first = second;
second = temp;
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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 n2 number of comparisons are made to sort a list of size n
• We therefore say that these sorts are of order n2
• Other sorts are more efficient: order n log2 n
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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
Aalborg Media LabApr 19, 2023
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 log2 N
Aalborg Media LabApr 19, 2023
Binary Search
-1 2 3 5 8 10 15
0 1 2 3 4 5 6
mid highlow
target
Aalborg Media LabApr 19, 2023
-1 2 3 5 8 10 15
0 1 2 3 4 5 6
mid highlow
target
Binary Search
Aalborg Media LabApr 19, 2023
Binary Search
-1 2 3 5 8 10 15
0 1 2 3 4 5 6
high
mid
low
target
Aalborg Media LabApr 19, 2023
Exercises
• Exercises– 9.1
• Programming Projects– 9.1 & 9.2