ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
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.
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.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
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.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism & Interfaces
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.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
© 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 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Polymorphism.
Object Oriented Programming
Designing for Inheritance
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Computer Programming with JAVA
Chapter 9 Polymorphism.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Overview °Problem: Can we delay decisions regarding which method to use until run time? °Polymorphism Different methods with the same name can be executed for the same reference at different times °Polymorphism via inheritance References of parent classes can be assigned addresses of child objects °Flexibility Java was written with the Internet in mind. The changing environment of the web necessitates flexibility

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Basic Reference Creation Cat cat = new Cat(); Animal animal = new Animal(); This involves creating a reference and then assigning an address of an object of the same type This seems pretty straightforward

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 More complicated example °What happens if Cat is a subclass of Animal We can assign the address of Cat to an Animal reference! °Animal animal = new Cat(); °An object reference of super class type points to an object of sub class type. °It is legal and it is called polymorphism. °Since Animal may have several subclasses, multiple types can be assigned to animal

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Binding °Consider the following method invocation: animal.makeNoise(); °At some point, this invocation is bound to the definition of the method that it invokes °If binding occurred at compile time, then that line of code would call the same method every time °Java defers method binding until run time -- this is called dynamic binding or late binding °Late binding provides flexibility in program design

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Polymorphism °The term polymorphism literally means "having many forms" °A polymorphic reference Variable that can refer to different types of objects at different points in time °The method invoked with a polymorphic reference can change from one invocation to the next °All object references in Java are potentially polymorphic

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Polymorphism °Suppose we create the following reference variable: Occupation job; °Java allows this reference to point to an Occupation object Also could point to any object of a subclass °This compatibility can be established using inheritance or using interfaces °Careful use of polymorphic references can lead to elegant, robust software designs

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 References and Inheritance °  An object reference can refer to an object of its class, Also, can refer to an object of any class related to it by inheritance °Example: The Holiday class is used to derive a class called Christmas A Holiday reference (‘day’, in this case) could be used to point to a Christmas object Holiday day; // day is a ref to Holiday object (Holiday object not yet created) day = new Christmas(); // Here, day is an object of type Christmas and hence points to Christmas. Holiday Christmas

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 References and Inheritance °Assigning a child object to a parent reference is considered to be a widening conversion This is what we did in the last slide. (a pointer which points to an object of a derived type.) °  The opposite: Assigning an parent object to a child reference can be done also, It is considered a narrowing conversion and must be done with a cast We won’t focus on this in this course °  The widening conversion is the more useful

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Polymorphism via Inheritance °In polymorphism: The type of the object being referenced, not the reference type, that determines which method is invoked. °In previous slides, day is pointing to an object of type Christmas! °It is very common to have a pointer to a base class pointing to a derived object.

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 More °Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it Christmas also has a method called celebrate. °Now consider the following invocation: day.celebrate(); °  During execution day refers to (points to) a Holiday object, It invokes the Holiday version of celebrate ; if it refers to a Christmas object, it invokes the Christmas version

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Create an array of every animal in the Zoo? Animal animal[] = new Animal[1000]; animal[0] = new Dog(); animal[1] = new Cat(); animal[2] = new Tiger(); animal[3] = new Lion(); ……

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Super class object in parameter list of an method public void performance(Animal animal) { animal.makeNoise(); } for (int i = 0; i < 1000; i++) { performance(animal[i]); } Consider the following code segment

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Polymorphism via Inheritance °Consider the following class hierarchy: StaffMember ExecutiveHourly VolunteerEmployee

ECE122 L22: Polymorphism Using Inheritance April 26, 2007 Summary °The basic idea here is that parent references can point to child object °The specific method which is accessed depends on the type of the child °This is very flexible since the method used depends on the flow of the program °Children of abstract classes can also be used in this fashion