0
Instanceof is a binary operator that tests whether an object is the subtype of a certain type. Ex:
Object fonte = e.getSource();
if (fonte instanceof JButton){
...
}
If getSource() returned information from a Jbutton the result of the expression will be true.
What I don’t understand is why the return of the expression is true, since the return of the getSource() is an object type, so every object is a subtype of Jbutton? In case I have:
Object fonte = e.getSource();
if (fonte instanceof JLabel){
...
}
Assuming that the getSource() now return information about a Jlabel, as happens the differentiation since source is always the type Object? Instanceof will test the contents of source?
It’s called
polimorfismo
. And every object implicitly inherits fromObject
. Understanding polymorphism, answer your question– Skywalker