Chapter 9: Polymorphism Coming up: Creating Objects Revisited.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
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.
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.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
“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.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
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.
Types in programming languages1 What are types, and why do we need them?
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
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.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
CSCI-383 Object-Oriented Programming & Design Lecture 17.
BY:- TOPS Technologies
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Polymorphism in Methods
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Creating Objects & String Class
Polymorphism.
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Designing for Inheritance
CMSC 202 Polymorphism.
slides created by Ethan Apter
Chapter 9 Polymorphism.
Inheritance and Polymorphism
Presentation transcript:

Chapter 9: Polymorphism Coming up: Creating Objects Revisited

Today’s lecture Review polymorphism examples

Polymorphism Polymorphism is an core object-oriented concept that allows us to create a variable with "more than one form" We've seen this throughout the semester: with inheritance, interfaces If an object is of type Student, where Student extends Person, we could say Object thing = getStudent(); Person person = getStudent(); Student jane = getStudent();

Design in Java Consider object.toString() The question is, what does the toString method actually do? That is, what does the code for toString look like when it is called on some object? The answer is, "it depends" It depends on what is the actual type of the object, in memory This may or may not be the type that the reference to the object was given

toString and polymorphism import java.util.*; public class Test{ public static void main(String[] args){ ArrayList list = new ArrayList(); list.add(new Object()); list.add(new String("Kinga")); list.add(new Integer(3)); list.add(new ArrayList()); for(Object o : list){ System.out.println(o.toString()); System.out.println(o.getClass()); } }

Will this work? import java.util.*; public class Test{ public static void main(String[] args){ ArrayList list = new ArrayList(); list.add(new Object()); list.add(new String("Kinga")); list.add(new Integer(3)); list.add(new ArrayList()); for(int i = 0; i < list.size(); i++){ String o = ((String)list.get(i)); System.out.println(o); }

Binding Consider the following method invocation: obj.toString(); At some point, this invocation is bound to the definition of the method that it invokes i.e., we figure out which toString we actually run 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

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

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

Which of the following are valid in Java? Person jane = new Person(); Person jane = new Object(); Person jane = new Person(); jane = new Student(); //child class* Student eric = new Student(); Person jane = eric; Student eric = new Student(); Person jane = (Person)eric; Student eric = new Person(); Person jane = new Person(); Student eric = jane; Person jane = new Person(); Student eric = (Student)jane; * Student is a child class of Person

Solution Java only honors an assignment where the contract between interfaces is honored Pick the most surprising/confusing example from the previous slide and see how the interface was/was not honored → or try it out in Eclipse

So, does Java even care about types? It seems like we can assign an object to a reference that is not the same type as the object itself Yes, we can! As long as it makes sense to…i.e., the contract is honored Java is smart It knows the actual type of the object in memory It allows you to be flexible and use any compatible types

Flexibility Can you think of an example where this might be useful? (How have we used it, already?) Imagine we have a collection of animals of different types… Lets us write specialized code for every animal, rather than writing messy code in one animal class that is supposed to handle all the different types of animals How does Java know what kind of animal it is when the speak method is called?

Polymorphism and Inheritance Widening conversion: Object object = new String("kinga"); Narrowing conversion (will compile): Person jane = new Person(); Student eric = (Student) jane; Illegal narrowing conversion: Person jane = new Person(); Student eric = jane; Why?

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

Question What is the difference between interfaces and inheritance?

How does Java know the type of an object at runtime? This information is stored in memory, along with the contents of the object Why can't this sort of thing be decided at compile time? A compiler can only check for certain things, and implementing polymorphism correctly isn't one of them

Imagine a collection of Animal s ArrayList animals = DB.getAnimals(); animals contains Elephant s, Lion s, and Bear s for(int i = 0; i < animals.size(); i++) ((Animal)animals.get(i)).speak(); Will call the specialized speak method Collections are a natural way to use polymorphism in Java What else could we want to do this array? Why are collections useful?

Collection interfaces ArrayList implements the Collection interface Also implements List, which extends Collection Why use a Collection ? Collections.sort(List) can sort an ArrayList according to the natural ordering of its elements Elements have a natural ordering if they’re Comparable

Why do we care about sorting? we're dealing with information Need some way to search it, Therefore need some way to index it, Therefore need some way to order the items (unless we want terribly long searches) Comparing and searching are at the heart of many applications Maintaining a sorted list helps these operations go faster