This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


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: 24187
    
  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: 24187
    
  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.
 
GeeCON Prague 2014
 
subject: Question on "instanceof"