5

I have an object object and I'm going to call it's method toString. How do I know in what exact class this method is implemented last?

For example if we have hierarchy:

class A /*extends Object */{                                                                                                              

}                                                                                                                      
class B extends A{                                                                                                              
    public String toString() {                                                                                         
        return "representation";                                                                                       
    }                                                                                                                  
}                                                                                                                      
class C extends B{                                                                                                              
}                                                                                                                      
class D extends C{                                                                                                              
}                                                                                                                      

and the object

Object object = new SomeClass(); //(A/B/C/D/Object)                                                                                

then for toString() I should get Object for Object and A but B for B, C and D

6
  • 1
    Huh? What is someMethod()? Where is the hierarchy? What does toString() have to do with anything? Commented Aug 8, 2014 at 7:48
  • 1
    You question is quite unclear to me. Commented Aug 8, 2014 at 7:48
  • Related (but without a good answer): stackoverflow.com/questions/12934675/…
    – Thilo
    Commented Aug 8, 2014 at 7:49
  • @OliCharlesworth, sorry, I've forgot to write extends that I mean to be here, fixed
    – RiaD
    Commented Aug 8, 2014 at 7:49
  • Ok, that's kind of what I assumed ;) So given object, you want to know (via reflection) whether object.toString() would call B.toString() or Object.toString()? Commented Aug 8, 2014 at 7:52

1 Answer 1

8

You can use the Method.getDeclaringClass() method:

...
private Class<?> definingClass(Class<?> clz, String method) throws NoSuchMethodException, SecurityException {
    Method m = clz.getMethod(method);
    return m.getDeclaringClass();
}

...

System.err.println(definingClass(A.class, "toString"));
System.err.println(definingClass(B.class, "toString"));
System.err.println(definingClass(C.class, "toString"));
System.err.println(definingClass(D.class, "toString"));

...

Result:

class java.lang.Object
class com.example.B
class com.example.B
class com.example.B

You need to extend the definingClass() method appropriately if you need to look up methods which have parameters.

Not the answer you're looking for? Browse other questions tagged or ask your own question.