Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. 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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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,
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
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.
Topic 4 Inheritance.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
Java Polymorphism. What we have learned Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?
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.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
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)
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Polymorphism and Abstract Classes
Polymorphism.
Polymorphism and Virtual Functions
Advanced Programming in Java
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Comp 249 Programming Methodology
ATS Application Programming: Java Programming
Object Oriented Programming
Computer Science II Exam 1 Review.
CMSC 202 Polymorphism.
Inheritance.
Java – Inheritance.
Polymorphism.
Chapter 8 Class Inheritance and Interfaces
Advanced Programming in Java
Presentation transcript:

Polymorphism

3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. 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.

Late binding AKA dynamic binding Binding – the process of associating a method definition with a method invocation Early binding – the method definition is associated with the method invocation when the code is compiled; AKA static binding Late 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 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.

Static binding & static methods public class Sale { //… public static void announcement ( ) { System.out.println( “This is the Sale class.” ); } public class DiscountSale extends Sale { //… public static void announcement ( ) { System.out.println( “This is the DiscountSale class.” ); }

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

Downcasting & upcasting

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

Casting What are casts?  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

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.