Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism.

Similar presentations


Presentation on theme: "Polymorphism."— Presentation transcript:

1 Polymorphism

2 3 main programming mechanisms that constitute OOP:
Encapsulation Inheritance 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 Binding Binding – the process of associating a method definition with a method invocation

5 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.

6 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

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)?

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 makes this work?

9 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!

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?

11 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().

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

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

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

15 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” );

16 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

17 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

18 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

19 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

20 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" ); }

21 Downcasting & upcasting

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

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

24 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

25 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

26 clone() method

27 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.

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

29 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.

30 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?

31 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?

32 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.

33 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!


Download ppt "Polymorphism."

Similar presentations


Ads by Google