Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference variable casting

 
Biju Mahadevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Anyone explain what is reference variable casting? I am not able understand the concept and use of it.
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!


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,
Bu.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
palm.dropCoconut();
}
 
chiranjeevi gunturu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dolly shah,
I can clear your doubt. Observe the code below.

now,
look at the below main method.



OutPut will be : Animal..
Cat...

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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic