This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using instanceof 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 "using instanceof" Watch "using instanceof" New topic
Author

using instanceof

ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50

class All{}
class Bll extends All{
public static void main(String[] args){
All myA = new Bll();
m2(myA);
}


public static void m2(All a){
if(a instanceof Bll)
((Bll)a).doBstuff();


}


public static void doBstuff(){
System.out.println("a refers to Bll");

}

}




In the above code an instance of super class is used to check instanceof subclass while downcasting.The above example is from K&B book.

But on page 286 in K&B book its says the supercalss instance results in false if used with subclass (instanceof operand.... type we are comparing the reference against).

Please someone help me in understanding this.
[ November 13, 2007: Message edited by: ashni Prakash ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Ashni,

Firstly, it's helpful to think of the "instanceof" operator as indicating an "IS-A" relationship. It's always true that a subclass object IS-A superclass object, but it's not always true that a superclass object IS-A subclass object. For example: a Banana is always a Fruit, but a Fruit isn't always a Banana.

In the code you provided, the method m2 looks like this:

For the sake of discussion, let's also assume that there exist two more classes:

When m2 gets invoked, you don't necessarily know that "a" is an All instance. You only know that "a" is an All reference... but in Java, a reference can point to an instance of any subclass of its reference type. So "a" can be an instance of All, an instance of Bll, or even an instance of Cll or Bmm. However, because m2 wants to do something with "a" that's specific to Bll and Bll's subclasses, it therefore performs the instanceof check to filter out the cases where "a" is actually an instance of All or Bmm. Remember: a Bll instance IS-A Bll and a Cll instance IS-A Bll, but an All instance is not a Bll and a Bmm is not a Bll.

Another way of looking at it: a subclass has all the visible methods of the superclass, but a superclass need not have all the visible methods of its subclasses. A superclass instance therefore isn't an instance of a subclass, because you can't always invoke a subclass method on a superclass instance.

Hope this helps a little. Do feel free to ask if you have any further questions!
[ November 13, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
Thank you very much kelvin,it helped me a lot.
Nice explaination
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using instanceof