File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variable casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variable casting" Watch "Reference variable casting" New topic

Reference variable casting

Biju Mahadevan

Joined: Sep 22, 2007
Posts: 10
Can Anyone explain what is reference variable casting? I am not able understand the concept and use of it.

He is the best Actor in the world.<br />I am a Big Fan of <a href="http://""" target="_blank" rel="nofollow">Superstar RajiniKanth.</a>
Prahlad Joshi
Ranch Hand

Joined: Apr 21, 2007
Posts: 44
class a{};
class b extends a{};
inteface i{};
class c implements i{};

a=new b();//it's called upcasting done implicitly
i=new c();//upcasting
b B=(b)a;//downcasting
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

When you extend a class, all subclasses inherit the methods of the superclass. Additionally, the subclasses may have additional (more specific) methods that the superclass does not have.

What method you can call depends only on the type of the variable.
When you have a variable of superclass type, you can store instances of the subclasses with this variable (Polymorphism). You have a reference to this subclass object, but you cannot use the specific subclass methods with the variable of the superclass.

But if you are sure that the instance referenced by the variable is really a subclass object, you can cast and apply the more specific method.

For example:

Here PalmTree has a more specific method (with the coconuts). Only PalmTree variables can invoke this method, not general Plants.
If you have a class that uses this class, e.g.

Therefore the bold line when uncommented would not work.
The plant-method works for all plant instances (and shows polymorphy due to overiding as in the alga).
If you want to invoke a PalmTree specific method, you can cast and invoke the palm-specific method as in the lines in italics.
The instanceof check is necessary to avoid ClassCastExceptions.

Output - by the way - is:

Hope that helped,

all events occur in real time
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Hi Hassel,
I am not getting following block of code from your post. How plant can be an instance of palmTree? plant is superclass of palmTree. Or I am missing something? Please explain me this concept as I am not good in casting related questions. Your explaination would help me.

if(plant instanceof PalmTree){
PalmTree palm = (PalmTree) plant;

SCJP-1.5<br />SCWCD-1.4
chiranjeevi gunturu
Ranch Hand

Joined: Mar 02, 2007
Posts: 59
Hi dolly shah,
I can clear your doubt. Observe the code below.

look at the below main method.

OutPut will be : Animal..

This is because instanceof test will return true if you are testing an object with its class or its parents class or above the parents class.

i wish this will make clear ..thank you
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Dolly wrote:
How plant can be an instance of palmTree? plant is superclass of palmTree. Or I am missing something?

Instead of a list (vector) I'll explain with just a variable:

Plant variable = new PalmTree();
System.out.println(variable instanceof PalmTree);
System.out.println(variable instanceof Plant);
System.out.println(variable instanceof Object);

prints three times "true".
You must understand the instanceof operator checking if the object refered by the variable is an instance (an object) of the type asked for.
In the above example variable "plant" is of type Plant, but you can assign also subtypes of Plant to this variable. With the instanceof operator, you can check which one hides behind the variable.

I agree. Here's the link:
subject: Reference variable casting
It's not a secret anymore!