Download presentation
Presentation is loading. Please wait.
Published byMarsha Walters Modified over 8 years ago
1
Polymorphism
2
3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism
3
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.
4
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.
5
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
6
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)?
7
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?
8
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!
9
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?
10
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().
11
Late binding Late binding is not “free.” Some additional overhead at runtime is required.
12
final Recall the final keyword. What happens for an instance variable? What happens for a method? What happens for a class?
13
Late binding exceptions Java does not use late binding with: Private methods Methods marked final Static methods Static binding is used instead.
14
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.” ); }
15
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” ); }
16
Downcasting & upcasting
17
Casting What are casts? Where have we seen/used casts before?
18
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);
19
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
20
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
21
clone() method
22
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.
23
Unofficial version of clone() public Class_Name clone ( ) { return new Class_Name( this ); } Later, we will define the “official” version.
24
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.
25
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?
26
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?
27
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.
28
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.