Originally posted by generator:
One method could be something like the following !![BOLD]Warning, untested code follows[/BOLD]
Code:
SomeClass sc = new SomeClass();
Object o = (Object) sc;
while (!(o instanceof Object)) {
o = o.super();
}
System.out.println(o);
Of course I'm not sure if you can use super from outside of the class like that, nor am I certain the casting will work right for using instanceof.
Nope, it won't work. "o instanceof Object" is always true in Java for any object reference you have (not sure about null, but that's beside the point anyway). What you mean is "o.getClass().equals(Object.class)". And "super" is not a fake method, but a fake member that is only accessible from inside the class anyway.
But, in any case, none of this will work anyway. Java maintains protection and security guarantees that ensure that if you call a method with a given signature on an object of a particular class, you will call the most-specific method (overridden in the 'lowest' class on the inheritance tree). If this were otherwise, you could, as a normal client of a class, muck up its internal representation in general and blow it to pieces.
Even reflection, AFAIK, won't get you what you want. You'll still end up calling the most-derived toString() method.
System.identityHashCode() is, however, for the problem in question, exactly what you want.