• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Question on "instanceof"

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


:roll: :roll:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic