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.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
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.
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.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
© 2006 Pearson Education Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Polymorphi sm. 2 Abstract Classes Java allows abstract classes – use the modifier abstract on a class header to declare an abstract class abstract class.
Chapter 9 Polymorphism. Chapter Scope The role of polymorphism Dynamic binding Using inheritance for polymorphism Exploring Java interfaces in more detail.
COS 312 DAY 14 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Report Over due – Next progress report is March 26 Assignment 4 Due Assignment 5.
© 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.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
COS 312 DAY 16 Tony Gauvin.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Abdulmotaleb El Saddik University of Ottawa
Polymorphism.
Chapter 7: Inheritance Java Software Solutions
Object-Oriented Programming: Polymorphism
Object Oriented Programming
Designing for Inheritance
Review for the Final Exam
Polymorphism Polymorphism
Chapter 9 Polymorphism.
Lecture 0311 – Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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 at different times if the object that obj points to changes  Polymorphic references are resolved at run time; this is called dynamic binding  Careful use of polymorphic references can lead to elegant, robust software designs  Polymorphism can be accomplished using inheritance or using interfaces

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

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  An Object reference can be used to refer to any object An ArrayList is designed to hold Object references

4 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

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

6 Polymorphism via Inheritance  Now consider the task of paying all employees  See Firm.java (page 412) Firm.java  See Staff.java (page 414) Staff.java  See StaffMember.java (page 416) StaffMember.java  See Volunteer.java (page 417) Volunteer.java  See Employee.java (page 418) Employee.java  See Executive.java (page 420) Executive.java  See Hourly.java (page 421) Hourly.java