• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Most specific method.......

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
^
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Animesh buddy ur code correction is also not giving
appropriate output
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic