This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JAVA Upcasting and Downcasting of reference variables  RSS feed

 
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.

 
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

 
Bartender
Posts: 4050
54
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: 4050
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to JavaRanch
 
Ranch Hand
Posts: 2908
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.
 
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: 4050
54
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!