Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Most specific method.......

 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

plz refer the code hereunder:



first of all my code is not compling, plz help me remove the bug....

secondly what should be the method selected here out of all the overloaded writeObject([an object param], [class object]) methods

thanx
amit
 
Simon Birch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The most obvious error in the code is the top line which should read


With this change the code compiles and runs fine for me.
 
Harish Kashyap
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method invoked in this case will always be

writeObject(Object obj, Class type)

the key to it lies in the invoking method

writeObject(Object obj)
^
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

secondly what should be the method selected here out of all the overloaded writeObject([an object param], [class object]) methods

As its usually that overloading is based on the reference type and not the exact object which is being referred. Sice ur reference type is Object, the method to be called is


Had ur writeObject method been:

The output would have been "I am a List"

Hope u got it.
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Resolved the code up to 1 error

import java.lang.util.*;
import java.io.*;

public class Test
{
public void writeObject(Object obj)
{
Test type = obj.getClass();
writeObject(Test test);
}
public void writeObject(Object obj,Test type)
{
System.out.println("I am an object");
}
public void writeObject(Integer obj,Test type)
{
System.out.println("I am an integer");
}
public void writeObject(List obj,Test type)
{
System.out.println("I am a List");
}
public void writeObject(Set obj,Test type)
{
System.out.println("I am a Set");
}
public static void main(String args[])
{
Test test = new Test();
test.writeObject(new ArrayList());
}
}
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Animesh buddy ur code correction is also not giving
appropriate output
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obj.getClass();

can i say that the above line will always result in returning the "reference type" of the obj.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deshdeep,

Animesh buddy ur code correction is also not giving
appropriate output

Buddy what its giving then? please let me know, may be i am doing wrong

When u do obj.getClass() it returns u an object of type Class. So according to the example it will return of type ArrayList.class
[ March 28, 2005: Message edited by: Animesh Shrivastava ]
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all i'd like to correct Deshdeep divakar for:
there is nothing like java.lang.util.* ....it is import java.util.*

secondly again Deshdeep, Animesh's code will fail in the rareest possibility so it actually gives o/p as "I am a List"

thirdly to answer the last question i'd say that getClass() returns the runtime class object of the object which calls the very same method, immp thing to note here is there is diff. in compile-time and run-time class in some cases.....

thanx to all and spc. Animesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic