• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on "instanceof"

 
Yuva Raj Sah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why the code's output is :
ture
true
true
true


:roll: :roll:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type of the variable "i" doesn't matter at all. All that matters is the type of the object it points to, which is class B. Just remember that "instanceof" is evaluated at runtime.

An instance of B is an instance of A because B extends A.

A B is a B, of course.

A B is an I1 because A implements I1, and B extends A.

And a B is an I2 because B implements I2.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type of the variable "i" doesn't matter at all. All that matters is the type of the object it points to, which is class B. Just remember that "instanceof" is evaluated at runtime.

An instance of B is an instance of A because B extends A.

A B is a B, of course.

A B is an I1 because A implements I1, and B extends A.

And a B is an I2 because B implements I2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic