Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Object Oriented Programming...function despatch  (Read 826 times)
0 Members and 1 Guest are viewing this topic.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Posted 2004-02-15 10:06:34 »

Age-old problem: you have two methods whose signatures only differ in one argument, and in one method the type of that argument is a super-class of the type of the same argument in the other method.

e.g.
1  
2  
3  
4  
5  
6  
public void method1( String a ) {} // 1
public void method1( MyExtendedString a ) {} // 2
}
...
class MyExtendedString extends String
{}


If you invoke:
1  
2  
3  
4  
5  
6  
MyExtendedString sub = new MyExtendedString( "blah" );
String parent = sub;

method1( parent ); // A
method1( sub ); // B
method1( (String) sub ); // C


you have the joy of not being entirely sure which method is going to get called. Obviously, A will be executed by 1, and B will be executed by 2.

The way it works is that C will then be executed by 1 (nb: if you like, I've got code that will demonstrate this happening).

My implementation of method 1 above checks if the argument is an instance of the subclass, and if so casts it to the subclass, and calls the same method again which, I would expect to cause method 2 instead to be invoked on the subclass - but it doesn't, it just calls itself again (JDK 1.4.2).

Is this right? You can cast a sub class to a super to change the function that's despatched, but you can't do vice versa? But if you pass in a valid sub class it WILL be despatched to the subclass-specific function? I regularly (if infrequently) get problems with differences between how I remember java's function despatcher and how it actually is, but I thought I had it pretty well sussed by now Sad.

malloc will be first against the wall when the revolution comes...
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2004-02-15 20:26:07 »

Since when can you extend String?  It's final.

*edit* BTW.. it works as expected for me if I extend an extendable class.  e.g. the cast makes the method with the more specific type be called.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
public class Test
{
      public static void main(String [] args)
      {
            Test x = new Test();
            A a = new A();
            B b = new B();
           
            x.m1( a );
            x.m1( b );
            x.m1( (A)b);
      }
     
      void m1( A s )
      {
            if( s instanceof B )
            {
                  System.out.println("casting...");
                  m1( (B) s );
            }
            else
                  System.out.println("m1 - A");
      }
     
      void m1( B s)
      {
            System.out.println("m1 -- B:");
      }
}
class A
{
}
class B extends A
{
}

Offline pepijnve

Junior Member




Java games rock!


« Reply #2 - Posted 2004-02-15 22:47:04 »

From the java spec on overloading:
Quote
When a method is invoked (§15.12), the number of actual arguments and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked (§15.12.2).

Because the compile time type is used, there's no uncertainty as to which version of the overloaded method will be invoked.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cfmdobbie

Senior Member




Who, me?


« Reply #3 - Posted 2004-02-17 10:17:27 »

As overloaded methods may have different return types it's a compile-time issue deciding what method gets called.

Hellomynameis Charlie Dobbie.
Offline sma

Junior Member





« Reply #4 - Posted 2004-02-22 07:24:36 »

You need a language with dynamic method dispatch (Lisp, Dylan just to name two) to achieve what you want - I think.

The usual workaround is double dispatch or using the visitor pattern.  That is, instead of

1  
2  
3  
4  
5  
6  
class Base {}
class Sub extends Base {}
class Util {
  void m(Base b) ...
  void m(Sub b) ...
}


use

1  
2  
3  
4  
5  
6  
7  
8  
9  
class Base {
  void accept() ...
}
class Sub extends Base {
  void accept() ...
}
class Util {
  void m(Base b) { b.accept(); }
}


This will correctly dispatch you message.

.: Truth Until Paradox!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (80 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.081 seconds with 21 queries.