I had a discussion with a coworker today, whether usage of using the Java operator instanceof
is a kind of reflection. And the discussion quickly evolved into what actually defines reflection.
So, what is the definition of reflection?
And is the usage of instanceof
considered "using reflection" ?
And in addition, if instanceof
is considered reflection, then is polymorphism not also "using reflection"? If not, what is the difference?
5 Answers 5
This is the definition of reflection according to wikipedia:
In computer science, reflection is the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.
I couldn't have said it better myself and highlighted the important part for your question. That said, yes, instanceof
is considered using reflection. The program observes its structure and conducts type introspection .
-
3@Steven Jeuris: The difference is that
if (true)
looks at a value, not at a type. That's why it's not considered reflection.sleske– sleske2011年09月07日 07:50:36 +00:00Commented Sep 7, 2011 at 7:50 -
1@sleske: The reason why I mentioned it was because observing a type is such a rudimentary principle. Without it there would be no polymorphism. Technically speaking this means a simple override could also be considered reflection (or at least results in). Do not forget the second part of the definition: and modify its own structure and behavior at runtime. All in all I upvoted this answer (check my second comment), but I do understand where the question from the OP came from. ;pSteven Jeuris– Steven Jeuris2011年09月07日 08:06:23 +00:00Commented Sep 7, 2011 at 8:06
-
1According to that definition of reflection, would normal polymorphism not also be a kind of reflection?Bjarke Freund-Hansen– Bjarke Freund-Hansen2011年09月07日 08:07:07 +00:00Commented Sep 7, 2011 at 8:07
-
4@bjarkef: I would say "no", as the virtual dispatch used by polymorphism is done implicitly by the language itself, rather than the application code explicitly examining the object in question to identify its characteristics.Dave Sherohman– Dave Sherohman2011年09月07日 08:31:14 +00:00Commented Sep 7, 2011 at 8:31
-
2@Dave Sherohman: I'd also add that most of the work (setting up jump tables, etc) is done at compile time, not run time.TMN– TMN2011年09月07日 17:00:09 +00:00Commented Sep 7, 2011 at 17:00
For the sake of clarity, I would consider two answers.
Theoretically, instanceof
is a form of reflection, as explained in Falcon's answer.
In computer science, reflection is the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.
However, practically, when a programmer talks about using reflection he usually refers to much more than just checking whether a variable is of a certain type. This is such a rudimentary concept, without which, polymorphism wouldn't be possible.
Do note that using instanceof
often indicates a code smell, and proper polymorphism can often be used instead.
-
1+1 for the potential code smell - not always, just the potentialGary– Gary2011年09月08日 09:33:46 +00:00Commented Sep 8, 2011 at 9:33
Since the other answers, the definition in Wikipedia has been changed (I happen to agree with this change, but I wasn't the one who made it) to remove the part in parentheses:
In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.
https://en.wikipedia.org/wiki/Type_introspection also specifically says
Introspection should not be confused with reflection
I wouldn't consider instanceof
to fall under the first definition: the program examines a value, not its own structure, and it doesn't involve any values representing program structure (in Java: java.lang.Class
, java.reflect.Method
, etc.); the hallmark of reflection is working with such values.
You might be interesting in the following article: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
The key in this article is a code snippet where they simulate the "instanceof" keyword by using the "isInstance" method of the "Class" class, which is part of the reflection feature of Java.
-
Nice explanation for Java.Falcon– Falcon2011年09月07日 07:49:30 +00:00Commented Sep 7, 2011 at 7:49
YES, "instanceof" operator its a form of reflection. The previous answers tell you why.
"Reflection" or "object / class introspection" is a hype these days, but several programming languages have use some of that concept, from some time.
DOT NET (Java clone), (mis) uses a lot.
-
1Uuuuhm, ... dude?Konrad Rudolph– Konrad Rudolph2011年09月07日 16:20:51 +00:00Commented Sep 7, 2011 at 16:20
-
3Your first sentence is correct, and redundant in the face of previous answers. The rest is just wrong.Frank Shearar– Frank Shearar2011年09月08日 08:49:54 +00:00Commented Sep 8, 2011 at 8:49
Explore related questions
See similar questions with these tags.
instanceof
is an example of reflection. Certainly it is somewhere between normal data use and metadata use viagetClass()
& friends, but you can have workable definitions wither way.return
considered "structural programming"?