Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA Upcasting and Downcasting of reference variables

 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not about Casting[though it is a UpCasting ]. it is about Polymorphism


is equal to

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to JavaRanch
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:And Welcome to JavaRanch


Thanks Vijitha Kumara and Sagar Rohankar
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I get this right?

Yep
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic