wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on "instanceof"" Watch "Question on "instanceof"" New topic
Author

Question on "instanceof"

Yuva Raj Sah
Greenhorn

Joined: Apr 28, 2004
Posts: 6


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


:roll: :roll:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on "instanceof"