Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
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.
Inheritance Inheritance Reserved word protected Reserved word super
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.
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.
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.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
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.
Chapter 9 Polymorphism. Chapter Scope The role of polymorphism Dynamic binding Using inheritance for polymorphism Exploring Java interfaces in more detail.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
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.
© 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.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism Lecture - 9.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Abdulmotaleb El Saddik University of Ottawa
Polymorphism.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Chapter 7: Inheritance Java Software Solutions
Object-Oriented Programming: Polymorphism
Designing for Inheritance
Lecture 23 Polymorphism Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Overriding Methods & Class Hierarchies
Chapter 9 Polymorphism.
Lecture 0311 – Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

NOTE: PA2 §To deal with the command line argument, you do not need a try/catch block. §Exceptions handling should be used for exceptional situations, not for things that you can solve more elegantly in other ways.

Polymorphism §from the Greek §poly = many §morph = forms

We have seen polymorphism §Think clocks. §You can create a Clock or an AlarmClock. §You can create a Clock and instantiate an AlarmClock. §The method used (like the updateTime method) will be based on the object type. §But the object declaration determines which methods are available to us. §See examplesexamples

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

Polymorphism §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(to be discussed later)

7 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

8 References and Inheritance  An Object reference can be used to refer to any object An ArrayList is designed to hold Object references §See the ArrayList in the Appendix M – pg 738

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 See page 485 for the full UML diagram

Polymorphism via Inheritance §Now consider the task of paying all employees  See Firm.java (page 486) Firm.java  See Staff.java (page 487) Staff.java  See StaffMember.java (page 489) StaffMember.java  See Volunteer.java (page 491) Volunteer.java  See Employee.java (page 492) Employee.java  See Executive.java (page 493) Executive.java  See Hourly.java (page 494) Hourly.java

Object Class §The Granddaddy of all classes. §We override the toString and equals methods commonly in creating our own classes. §Question – Is it possible for us to write code that will do something different based on the class of the object instead of the class in which the variable is declared. Like, can we prevent the run time errors we saw in the Executive vs Hourly classes.

Class class §Object class has a getClass method that returns the Class of the object. §The Class class has a getName method which returns a String equivalent to the name of the Class. §Using these two, we could write code that will do one thing for one class of object and another for a different class of object.

Meanings of Polymorphism  In this course:  At run-time, a variable may "take the form of" any of its descendants.  Related Concepts: Dynamic binding

Polymorphism in Java  Class Membership  The compiler uses the declared class  At run-time an object knows its class Search Order: If the "message" is sent to an object of the derived class then the derived class is searched (for the existence of such a method) first and the base class is searched second. (Note: The search will move up the class hierarchy until as needed.) If the "message" is sent to an object of the base class then only the base class is searched.