Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downcasting And Upcasting

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 ??
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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)...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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).
 
Simone Aiello
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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?!
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • 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.)
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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.
 
See Furst
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic