Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Most specific method....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Most specific method......." Watch "Most specific method......." New topic
Author

Most specific method.......

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Dec 26, 2003
Posts: 21
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

Joined: Jun 14, 2000
Posts: 118
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

Joined: Jul 19, 2004
Posts: 298

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

Joined: Apr 19, 2004
Posts: 91
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());
}
}


"Do not be afraid of going slow, be afraid of standing still"
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Hi,
Animesh buddy ur code correction is also not giving
appropriate output
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
obj.getClass();

can i say that the above line will always result in returning the "reference type" of the obj.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 05, 2005
Posts: 206
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Most specific method.......
 
Similar Threads
instanceof Doubt again??
Overloading doubt
generics with polymorphism
LinkedList problem
Difference between Null String and Null Object