• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Reference variable casting

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can Anyone explain what is reference variable casting? I am not able understand the concept and use of it.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't listen to Steve. Just read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic