Presentation is loading. Please wait.

Presentation is loading. Please wait.

Double Dispatching In Java. Single Dispatch  Uses polymorphism between base and derived classes  Compiler registers container pointer to base class.

Similar presentations


Presentation on theme: "Double Dispatching In Java. Single Dispatch  Uses polymorphism between base and derived classes  Compiler registers container pointer to base class."— Presentation transcript:

1 Double Dispatching In Java

2 Single Dispatch  Uses polymorphism between base and derived classes  Compiler registers container pointer to base class  At run-time a dispatch vector is used to determine what the container is actually pointing to

3 Single Dispatch  This can be either an object of base or derived type  But, if calling a method dynamically the signatures of the base and derived methods must be identical  If they are not the run-time system uses the static, not the dynamic type to determine the call.

4 An Example  My apartment in Yerevan has ants and cockroaches  I want to spray the ants with ant poison and the cockroaches with cockroach poison  The Public Health Department at the AUA comes up with the following hierarchy:

5 GenericPoison AntPoisonCockroachPoison Insect AntCockroach

6 Single Dispatch  Poison is a kind of visitor to Insect  We want to have a method spray() in all three classes that is insect-specific  Start with the base class Poison  Use Insect as the parameter type of spray()

7 class Poison { public void spray(Insect a Bug){ …} } class AntPoison { public void spray( ???){…} }

8 Single Dispatch  Problem: we would like to spray ants with ant poison  Hence, if I have a declared Insect pointer that has a reference at run-time to an Ant object I would like to call the Ant’s spray() method  What is the parameter type for the Ant spray()?

9 First Attempt  Try directly to apply ant poison to an ant: public class Poison { public void spray(Insect aBug){ System.out.println(“Generic Spray”); }; Public class AntPoison extends Poison { public void spray(Ant anAnt){ System.out.println(“Spraying ant with ant spray”); }; public class Insect{…}; public class Ant{…};

10 Now try it  Make a container and try polymorphism: Poison container = new AntPoison(); container.spray(new Ant());  Produces Generic Spray  !!!!!  What happened?

11 Just this There is no method in Poison with the parameter of type Ant. There is no method in Poison with the parameter of type Ant. Therefore the Java run-time system reverts back to the static definition of the polymorphic container, i.e. Poison Therefore the Java run-time system reverts back to the static definition of the polymorphic container, i.e. Poison

12 Cheap way out:  Make the container point to AntPoison : AntPoison container = new AntPoison(); Container.spray(new Ant()); Container.spray(new Insect());  Produces Spraying ant with ant spray Generic Spray

13 Caveat  There is no method in AntPoison to spray a generic insect  Hence the compiler reverts to the spray method in the super class, i.e. in poison  Way out: Need a spray method in Poison with an Ant parameter:

14 public class Poison{ public void spray(Insect aBug){ System.out.println(“Generic Spray”); } public void spray(Ant antBug){ System.out.println(“Generic Ant Spray”); } }; public class AntPoison extends Poison{ public void spray(Ant antBug){ System.out.println(“Spraying Ant with Ant spray”); } };

15 Test This Poison container = new AntPoison(); Insect bugType = new Ant(); container.spray(bugType); bugType = new Insect(); container.spray(bugType);Produces Generic Spray

16 Why?  Because there is no spray method in Ant with an insect parameter  But, calling Poison container = new AntPoison(); Container.spray(new Ant()); Container.spray(new Insect());

17 Single Dispatch Produces Produces Spraying ant with ant spray Generic spray  Which at least works on ants.  Doesn’t work properly on insects

18 Problem We are only spraying with generic poison We are only spraying with generic poison In order to make use of a spray method in the AntPoison class we must repeat the spray() method with exactly the same signature in the AntPoison derived class In order to make use of a spray method in the AntPoison class we must repeat the spray() method with exactly the same signature in the AntPoison derived class Furthermore, we need a beingSprayed() method of each poison type in both Insect and Ant Furthermore, we need a beingSprayed() method of each poison type in both Insect and Ant


Download ppt "Double Dispatching In Java. Single Dispatch  Uses polymorphism between base and derived classes  Compiler registers container pointer to base class."

Similar presentations


Ads by Google