• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using instanceof

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

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 ]
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much kelvin,it helped me a lot.
Nice explaination
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic