Popular Tags

When java calls methods through interfaces, how do you know which implementation class is in the calling class.

92 5

An abstract interface, with many implementation classes, when calling a method through an interface, how to know which implementation class is called?

Thank you.

2014-07-02 update

And the first three answers are 可以, but I still haven't found the answer I want. For example, looking at someone else 's in Eclipse, it's possible to see the source of the method in the interface if you go directly to the source by pressing ctrl, and?

5 Answers

60 3

Get its class with an object 's getClass() method, and then you'll be able to determine which implementation class this is.

Polymorphism is the concept of runtime, and it isn't possible to know exactly which implementation to use before running. What you can do is find all the classes that implement this interface and then guess what's going on.

57 4

It can also be used to determine

134 5

Reflection or creates all classes in a single instance with a hashmap store

112 1

In theory, we can use reflection.

But I understand that eclipse doesn't support what you want.
The reason is that the nature of a polymorphism makes the object 's real type only available to runtime ( runtime ). Most refactoring and declaration jumping for eclipse are based on semantics, which can only rely on information obtained at compile time ( compile time ). So the information acquired at compile time is sometimes inferred only to interface or superclass so that the real type can't be determined at compile time. Eclipse doesn't help, intellij.

85 1
...