• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downcasting And Upcasting

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the use of Downcasting and Upcasting ?

DOWNCAST


It allows to call methods od Dog class not defined in Animal


UPCAST



It allows to call methods of Animal not defined in Dog and
Methods overriden by Dog.
It is similar to

so, why do we upcast ??
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search google for "Program to an interface not an implementation" (basically upcasting is redundant as it happens implicitly but we use supertype references instead of subclass references due to programming best practices)...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
UPCAST



It allows to call methods of Animal not defined in Dog and
Methods overriden by Dog.

No it still calls the methods in Dog and not in Animal (except for static methods).
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

UPCAST



It allows to call methods of Animal not defined in Dog and
Methods overriden by Dog.
It is similar to

so, why do we upcast ??

Which are methods of Animal not defined in Dog?!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must have created the method of Calender class or any other class which are abstract using the factory methods:



The follwing doubt came to my mind while studying-
"Calender class is an abstract, we cannot create an instance of that class. But in the above code Calender class is getting instantiated. HOW ON EARTH"

What actually happens is a subclass(of Calender) instance is created and returned by getInstance() method ans is assigned to the Calender reference c. This is an example of upcasting. We are able to use the methods of calender class by this method.(Correct me if I am wrong, if it is not an example of upcasting, but just an example of assigning a subclass object to superclass reference.)
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting plays major role during polymorphism. Polymorhically you will send sub classes to super classes and downcast to your sub-class.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:

mohitkumar gupta wrote:
UPCAST



It allows to call methods of Animal not defined in Dog and
Methods overriden by Dog.

No it still calls the methods in Dog and not in Animal (except for static methods).



I think that's what he meant.. If Animal has a fart() then a new Dog() can fart(), but it's defined in Animal, not Dog. However if both Dog and Animal define fart() the implementation in Dog is executed..

Still if you do:



and Dog implements howl();

a.howl() will crash

I just finished posting on what's a definition vs. implementation..Oracle and I disagree... What are people's takes? What does it mean to "define" a method vs. "implement" it?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic