This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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 Elasticsearch in Action this week in the Big Data 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: 24189
    
  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: 24189
    
  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.
 
jQuery in Action, 2nd edition
 
subject: Question on "instanceof"