Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Advertisements

Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 23. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 23 Problem set 4 Due Problem set 5 Posted (Last one)  Due Dec 8 Capstones.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
1 Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods.
© 2006 Pearson Education Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Searching and Sorting Searching algorithms with simple arrays
Chapter 5: Enhancing Classes
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Polymorphism.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Designing for Inheritance
Outline Late Binding Polymorphism via Inheritance
Chapter 9 Polymorphism.
Sorting and Searching -- Introduction
Arrays.
Presentation transcript:

Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs Chapter 9 focuses on:  defining polymorphism and its benefits  using inheritance to create polymorphic references  using interfaces to create polymorphic references  using polymorphism to implement sorting and searching algorithms  additional GUI components

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and Color Choosers Sliders

Binding Consider the following method invocation: obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, then that line of code would call the same method every time However, Java defers method binding until run time -- this is called dynamic binding or late binding Late binding provides flexibility in program design

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 The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic

Polymorphism Suppose we create the following reference variable: Occupation job; Java allows this reference to point to an Occupation object, or to any object of any compatible type This compatibility can be established using inheritance or using interfaces Careful use of polymorphic references can lead to elegant, robust software designs

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and Color Choosers Sliders

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 class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday day; day = new Christmas(); Holiday Christmas

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 Assigning an parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast The widening conversion is the most useful

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

Polymorphism via Inheritance Consider the following class hierarchy: StaffMember ExecutiveHourly VolunteerEmployee

Polymorphism via Inheritance Now let's look at an example that pays a set of diverse employees using a polymorphic method See Firm.java (page 488) Firm.java See Staff.java (page 489) Staff.java See StaffMember.java (page 491) StaffMember.java See Volunteer.java (page 493) Volunteer.java See Employee.java (page 494) Employee.java See Executive.java (page 495) Executive.java See Hourly.java (page 496) Hourly.java

Final Lecture! HW3  Due Friday at 5pm (not midnight) Final Exam  Comprehensive (all material covered in lecture since first day)  Closed book  Spread out in room; no one beside each other  Thursday: 4:15pm-6:45pm  Format 50 multiple choice; 1 point each Remaining 50 points from coding and essay questions

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and Color Choosers Sliders

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();

Polymorphism via Interfaces Suppose two classes, Philosopher and Dog, both implement the Speaker interface, providing distinct versions of the speak method In the following code, the first call to speak invokes one version and the second invokes another: Speaker guest = new Philospher(); guest.speak(); guest = new Dog(); guest.speak();

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and Color Choosers Sliders

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 There are many algorithms, which vary in efficiency, for sorting a list of items We will examine the Selection sort algorithms:  The book also discusses Insertion Sort, but you are not responsible for that

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  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

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

Swapping The processing of the selection sort algorithm includes the swapping of two values Swapping requires three assignment statements and a temporary storage location: temp = first; first = second; second = temp;

Polymorphism in Sorting Recall that an class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects We can use polymorphism to develop a generic sort for any set of Comparable objects The sorting method accepts as a parameter an array of Comparable objects That way, one method can be used to sort a group of People, or Books, or whatever

Selection Sort The sorting method doesn't "care" what it is sorting, it just needs to be able to call the compareTo method That is guaranteed by using Comparable as the parameter type Also, this way each class decides for itself what it means for one object to be less than another See PhoneList.java (page 502) PhoneList.java See Sorting.java (page 503), specifically the selectionSort method Sorting.java See Contact.java (page 505) Contact.java

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and Color Choosers Sliders

Searching Searching is the process of finding a target element within a group of items called the search pool The target may or may not be in the search pool We want to perform the search efficiently, minimizing the number of comparisons Let's look at two classic searching approaches: linear search and binary search As we did with sorting, we'll implement the searches with polymorphic Comparable parameters

Linear Search A linear search begins at one end of a list and examines each element in turn Eventually, either the item is found or the end of the list is encountered See PhoneList2.java (page 510) PhoneList2.java See Searching.java (page 511), specifically the linearSearch method Searching.java

Binary Search A binary search assumes the list of items in the search pool is sorted It eliminates a large part of the search pool with a single comparison A binary search first examines the middle element of the list -- if it matches the target, the search is over If it doesn't, only one half of the remaining elements need be searched Since they are sorted, the target can only be in one half of the other

Binary Search The process continues by comparing the middle element of the remaining viable candidates Each comparison eliminates approximately half of the remaining data Eventually, the target is found or the data is exhausted See PhoneList2.java (page 510) PhoneList2.java See Searching.java (page 511), specifically the binarySearch method Searching.java

Summary Chapter 9 has focused on:  defining polymorphism and its benefits  using inheritance to create polymorphic references  using interfaces to create polymorphic references  using polymorphism to implement sorting and searching algorithms  additional GUI components

Recommendation We did not get to cover the material in chapter 10. If you are a CS major, you should read this chapter some time at your leisure after the course is over. Please feel free to ask me questions about that chapter.

Redo from Chapter 6 Comments on Parameter passing  Pages

Objects as Parameters Parameters in a Java method are passed by value A copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Therefore passing parameters is similar to an assignment statement When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

Passing Objects as Parameters When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other objA.doThis( objActualParam ); doThis( Object objFormalParam ) { … }; The object. example  r3 = r1.add(r2); r2 object of type Rational memory address is what is passed

Passing Objects to Methods What a method does with a parameter may or may not have a permanent effect (outside the method) See ParameterTester.java (page 331)ParameterTester.java See ParameterModifier.java (page 333)ParameterModifier.java See Num.java (page 334)Num.java Note the difference between changing the internal state of an object versus changing which object a reference points to

Parameter tracing Step 1 Before invoking changeValues Step 2 tester.changeValues (a1, a2, a3); a1 a2 a3 f1 f2 f a1 a2 a3 f1 f2 f = undefined

Parameter tracing Step 3 f1 = 999; Step 4 f2.setValue (888); a1 a2 a3 f1 f2 f a1 a2 a3 f1 f2 f

Parameter tracing Step 5 f3 = new Num (777) ; Step 6 After returning from changeValues a1 a2 a3 f1 f2 f a1 a2 a3 f1 f2 f

pass by value - pass by reference Step 2 tester.changeValues (a1, a2, a3); a1 a2 a3 f1 f2 f pass by reference pass by value 111 pass by value pass by reference data variable objects

Arrays as Parameters An entire array can be passed as a parameter to a method Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other Therefore, changing an array element within the method changes the original An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type