Ida
Ida

Reputation: 2999

How can I get the method inheritance info?

Suppose class Dog extends abstract class Animal and implements class Play. Then, in class Dog, it implements the inherited abstract method Animal.eat() and Play.jump(). Imagine in Eclipse, there will be a little triangle on the left of Dog.eat() and Dog.jump(). And if you put your mouse on the little triangle, it will tell you that this method implements which abstract method from which class.

I was wondering how this method inheritance info is obtained. And my question is: if I have a set of .java files, without any help of IDE, how can I extract the same method inherit info as Eclipse does (like this method implements which abstract method from what class)?

I can think of one approach: given class Dog, I first obtain the inherit info of this class, Animal and Play. Then I compare all the method signature from Dog and Animal/Play and see which one implements what. But this approach seems a little naive, and may not be very efficient. So please let me know if there is any good approach that I can use to obtain the method inheritance info.

Thanks!

Upvotes: 0

Views: 198

Answers (2)

hanumant
hanumant

Reputation: 1101

You can use java Reflection to find out the superclass.

Upvotes: 2

Chris Thompson
Chris Thompson

Reputation: 35598

I think the approach you've outlined is the only way to do it. I could be wrong about this, but the only way to know which methods are inherited from a given interface/class is to examine the interface and consequently, you must search a given interface for a given method at one point or another. I would imagine that eclipse builds a list of methods based on the inheritance structure and when you hover over a method in a given class, consults that table to see which class/interface it came from. That last part is merely a guess though.

Upvotes: 1

Related Questions