Polymorphism.

Slides:



Advertisements
Similar presentations
Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“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,
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
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.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Polymorphism and Abstract Classes
Polymorphism.
Advanced Programming in Java
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Comp 249 Programming Methodology
ATS Application Programming: Java Programming
Object Oriented Programming
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
CMSC 202 Polymorphism.
Inheritance.
Polymorphism, Abstract Classes & Interfaces
Java – Inheritance.
Polymorphism CT1513.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Advanced Programming in Java
Presentation transcript:

Polymorphism

3 main programming mechanisms that constitute OOP: Encapsulation Inheritance Polymorphism

Polymorphism The ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding. Allows one to make changes in the method definition for the derived classes and have those changes apply to the software written in the base class.

Binding Binding – the process of associating a method definition with a method invocation

Binding types early/static binding late/dynamic binding the method definition is associated with the method invocation when the code is compiled late/dynamic binding the method invocation is associated with the method invocation when the method is invoked (at run time) Java uses late binding except for a few cases.

Late binding example class Figure draw() method that draws a point center() method that moves the object to the center of the screen and calls draw() superclass for drawing with the following subclasses: Rectangle draw() method that draws a rectangle Circle draw() method that draws a circle Oval draw() method that draws an oval

Late binding example We add a new subclass of Figure called Triangle. Do we need to recompile Figure (because Figure’s center() method will call Triangle’s draw() method)?

Late binding example We add a new subclass of Figure called Triangle. Do we need to recompile Figure (because Figure’s center() method will call Triangle’s draw() method)? NO! What mechanism makes this work?

Late binding example We add a new subclass of Figure called Triangle. Do we need to recompile Figure (because Figure’s center() method will call Triangle’s draw() method)? NO! What mechanism make this work? Late binding!

Late binding example What would happen (when Figure’s center() calls draw() for a Triangle) if we didn’t have late binding but had early binding instead?

Late binding example What would happen (when Figure’s center() calls draw() for a Triangle) if we didn’t have late binding but had early binding instead? Figure’s draw() would be called instead of Triangle’s draw().

Late binding Late binding is not “free.” Some additional overhead at runtime is required.

final Recall the final keyword. What happens for an instance variable? What happens for a method? What happens for a class?

Late binding exceptions Java does not use late binding with: Private methods Methods marked final Static methods Static binding is used instead.

Example class Sale { public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public void showAd ( ) { System.out.println( “buy sale” ); class DiscountSale extends Sale { System.out.println( “This is the DiscountSale class.” ); System.out.println( “buy discount sale” );

public class SaleTest { public static void main ( String args[] ) { Sale s = new Sale(); DiscountSale d = new DiscountSale(); s.announcement(); d.announcement(); s.showAd(); d.showAd(); s = d; System.out.println( s.toString() ); } class Sale { public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public void showAd ( ) { System.out.println( “buy sale” ); class DiscountSale extends Sale { System.out.println( “This is the DiscountSale class.” ); System.out.println( “buy discount sale” ); This is the Sale class. This is the DiscountSale class. buy sale buy discount sale DiscountSale@ad3ba4

public class SaleTest { public static void main ( String args[] ) { Sale s = new Sale(); DiscountSale d = new DiscountSale(); s.announcement(); d.announcement(); s.showAd(); d.showAd(); s = d; System.out.println( s.toString() ); } class Sale { public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public void showAd ( ) { System.out.println( “buy sale” ); class DiscountSale extends Sale { System.out.println( “This is the DiscountSale class.” ); System.out.println( “buy discount sale” ); This is the Sale class. This is the DiscountSale class. buy sale buy discount sale DiscountSale@ad3ba4

public class SaleTest { public static void main ( String args[] ) { Sale s = new Sale(); DiscountSale d = new DiscountSale(); s.announcement(); d.announcement(); s.showAd(); d.showAd(); s = d; System.out.println( s.toString() ); } class Sale { public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public void showAd ( ) { System.out.println( “buy sale” ); class DiscountSale extends Sale { System.out.println( “This is the DiscountSale class.” ); System.out.println( “buy discount sale” ); This is the Sale class. This is the DiscountSale class. buy sale buy discount sale DiscountSale@ad3ba4

public class SaleTest { public static void main ( String args[] ) { Sale s = new Sale(); DiscountSale d = new DiscountSale(); s.announcement(); d.announcement(); s.showAd(); d.showAd(); s = d; System.out.println( s.toString() ); } class Sale { public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public void showAd ( ) { System.out.println( “buy sale” ); class DiscountSale extends Sale { System.out.println( “This is the DiscountSale class.” ); System.out.println( “buy discount sale” ); This is the Sale class. This is the DiscountSale class. buy sale buy discount sale DiscountSale@ad3ba4

Static vs. dynamic binding example class TestStaticBinding { public static void main ( String args[] ) { Test t1 = new Fred(); t1.stat(); t1.notStat(); Fred f = (Fred) t1; f.stat(); } class Test { static void stat ( ) { System.out.println( "this is TestStaticBinding1" ); } void notStat ( ) { System.out.println( "this is TestStaticBinding2" ); } class Fred extends Test { static void stat ( ) { System.out.println( "this is Fred1" ); } void notStat ( ) { System.out.println( "this is Fred2" ); }

Downcasting & upcasting

Casting What are casts? Where have we seen/used casts before?

Casting What are casts? Where have we seen/used casts before? Converting from one type to another Where have we seen/used casts before? double d = 0.9; int i1 = (int) d; int i2 = (int) (d + 0.5);

Downcasting and upcasting Upcast = assigning an object of a derived class to a variable of a base class (or any ancestor class) straightforward Downcast = a type cast from a base class to a derived class (or from any ancestor class to any descendent class) troublesome

Downcasting When impossible, it will generate an error at either compile time or a run time. Required by equals() method (when downcasting from Object) instanceof may be used to check if downcast will work

clone() method

clone() method defined in Object as: protected Object clone() every object inherits a clone() method (supposed to) return a deep copy of the calling object you are expected to override it like a copy ctor but there are cases where clone() works but the copy ctor does not.

Unofficial version of clone() public Class_Name clone ( ) { return new Class_Name( this ); } Later, we will define the “official” version.

Cloning array elements public static Sale[] goodCopy ( Sale a[] ) { Sale b[] = new Sale[ a.length ]; for (int i=0; i<a.length; i++) b[i] = a[i].clone(); return b; } Does this work? Yes. Does this provide a “deep” copy? Yes, as long as clone() does.

Cloning array elements public static Sale[] goodCopy ( Sale a[] ) { Sale b[] = new Sale[ a.length ]; for (int i=0; i<a.length; i++) b[i] = a[i].clone(); return b; } Does it work if elements of a[] are not Sale objects but are derived from Sale?

Cloning array elements public static Sale[] goodCopy ( Sale a[] ) { Sale b[] = new Sale[ a.length ]; for (int i=0; i<a.length; i++) b[i] = a[i].clone(); return b; } Does it work if elements of a[] are not Sale objects but are derived from Sale? Yes. Why?

Cloning array elements public static Sale[] goodCopy ( Sale a[] ) { Sale b[] = new Sale[ a.length ]; for (int i=0; i<a.length; i++) b[i] = a[i].clone(); //polymorphic return b; } Does it work if elements of a[] are not Sale objects but are derived from Sale? Yes. Why? Because clone() is overridden.

Cloning array elements Does it work using a copy ctor? public static Sale[] goodCopy ( Sale a[] ) { Sale b[] = new Sale[ a.length ]; for (int i=0; i<a.length; i++) b[i] = new Sale( a[i] ); //not polymorphic return b; } This doesn’t work for subclasses of Sale!