Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1121 times)
0 Members and 1 Guest are viewing this topic.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Posted 2004-02-15 09: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


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2004-02-15 19: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 Duke




Java games rock!


« Reply #2 - Posted 2004-02-15 21: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 Duke


Medals: 1


Who, me?


« Reply #3 - Posted 2004-02-17 09: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 Duke





« Reply #4 - Posted 2004-02-22 06: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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (31 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!