File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes JAVA Upcasting and Downcasting of reference variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAVA Upcasting and Downcasting of reference variables" Watch "JAVA Upcasting and Downcasting of reference variables" New topic
Author

JAVA Upcasting and Downcasting of reference variables

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Hello,

I am having trouble understanding Java reference variable casting. Could anybody please explain me the concept.

Also, please see the below example:



Output from above code:
In callme of Dog

I was expecting "In callme of Animal". What am I missing?

When I say (classname)referencevariable, what exactly happens?

Thanks in advance for your help.


SCJP6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

it is not about Casting[though it is a UpCasting ]. it is about Polymorphism


is equal to

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

When you invoke the method "callme()" on a "Animal" reference, method from the Dog object is called because by casting you are just change the reference to type "Animal" though the actual object being an instance of "Dog". Actually you don't need an explicit cast as "Animal" is the super class of "Dog", it is perfectly valid to have "Animal" reference refers to "Dog" object (this is called Polymorphism). Since you have overriden the method "callme()" defined in "Animal" within the "Dog" class, method from the "Dog" class is invoked at runtime.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

And Welcome to JavaRanch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Welcome to JR.
Anu Kota wrote:
I was expecting "In callme of Animal". What am I missing?

For such problem, I follow my rule of thumb, like
Whenever a reference is used to call a method, then method belongs the class of that object gets called. Here "a" is of type "Animal" but it points to object of "Dog".

But as paramagnetism said, its polymorphism.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
A superclass reference can point to a subclass object.
This is how polymorphism is achieved in java.
Don't worry about it.

Read about java inheritance and polymorphism better.

One book (the book that i read) is java2 complete reference with really good examples.Please try that


To err is human,
To forgive is not company policy
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Vijitha Kumara wrote:And Welcome to JavaRanch


Thanks Vijitha Kumara and Sagar Rohankar
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
By casting, either up or down, I am changing the reference type only. That effects which methods can be called. If I upcast, methods in the superclass that can be inherited only can be called. If I downcast, methods that are specific to subclass can also be called. But which version of the overridden method gets called depends on the actual instance (polymorphism).



Eg:1 Upcast:
Dog d = new Dog();
((Animal)d).callme(); //Compiles, this invokes callme from Dog class.
((Animal)d).callme2(); //Compilation error

Eg:2 Downcast:

Animal a = new Dog();
a.callme(); //Compiles, this invokes callme from Dog class.
a.callme2(); //compilation error - thats why downcasting is needed
((Dog)a).callme2(); //Complies, invokes callme2 from Dog

Did I get this right?


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

Did I get this right?

Yep
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Thanks everybody for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVA Upcasting and Downcasting of reference variables