Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Java Generics.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Java Implementation: Part 3 Software Construction Lecture 8.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Programming With Java ICS Chapter 8 Polymorphism.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
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:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
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 5:Interfaces and Abstract Classes Michael Hsu CSULA.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Polymorphism.
Advanced Programming in Java
Polymorphism.
Classes in C++ C++ originally called "C with classes":
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
Designing for Inheritance
Chapter 9 Inheritance and Polymorphism
Polymorphism Polymorphism - Greek for “many forms”
Polymorphism.
Chapter 11 Inheritance and Polymorphism Part 2
Subtype Substitution Principle
Advanced Programming in Java
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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

Example Perfectly legal: Shape s1; Rectangle r = new Rectangle(…); s1=r; Shape s2 = new Rectangle(…); Object o = new Circle(…); Not legal: Shape s = new Circle; Rectangle r = s;

What Gets Checked at Compile-Time? Validity of assignments Can cause non-obvious restrictions: Shape s = new Rectangle(…); Rectangle r = s; \\ is this still a rectangle? Checking validity of assignments at compile time lets programs run faster

Casting If you really need a more specific type, you can do this: Shape s = new Rectangle(…); Rectangle r = (Rectangle) s; But this can be dangerous, and should generally be avoided

Late Binding: Which Method? Consider: Shape s = new Rectangle(…); String myOutput = s.toString(); If toString was defined in Shape then overridden in Rectangle, which definition should be used?  s is a Shape reference: Use the one from Shape ?  s actually refers to a Rectangle Use the one from Rectangle ?

Which Method? Java follows the reference to the actual instance  the reference is just pointing to the thing we use The instance method is what gets used  Rectangle This is called late binding… Java doesn’t decide which method gets called until the method is encountered at run-time

Why? Let’s you use a more generic reference when needed, but still get the right reference public void draw(Collection shapes) { for(Shape s: shapes) s.draw(); } Different shapes will have different draw methods  … but this will use the right one in each case

Also Works with Interfaces public boolean startsWith(List list, int val) { return list.get(0)==val; } Whether the argument is an ArrayList, Vector, or LinkedList, this method works  uses the.get(…) method from the appropriate underlying implementation

Late Binding Works Everywhere Interface, Abstract Class, Non-Abstract Class  can declare a reference to any of these  any object that inherits/implements the reference type can be used for the reference  any method that is defined by the reference can be used  the implementation in the actual instance will be called

A Caveat from the Text Consider interface Speaker, with method speak() Suppose Dog and Philosopher both implement Speaker Speaker guest; guest = new Philosopher(); guest.speak(); guest = new Dog(); guest.speak();

A Caveat from the Text Now suppose Philosopher also includes a method pontificate() Then this does not work: Speaker special = new Philosopher(); special.pontificate(); Method called must be in the interface

Example: The Coffee Bar Ordering at a coffee bar  espressos, coffee, low-fat milk, etc…  lots of structure, lots of subtypes, lots of polymorphism We want to represent orders taken, these can be passed on to make the drinks

How Hard is This? © Starbucks 2005, “Make it Your Drink”

Stuff to Represent Cup (to-go, for-here, iced, personal cup) Size (small, medium, large) Shots (1 or more espresso, caf/decaf)  default determined by size, but can be changed Syrups (0 or more flavour shots) Milk (whole, 2%, skim, soy) Toppings (whipped cream, caramel) Drink (espresso shot, Americano, mocha…)

Class Hierarchy

Implementation ….