aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Downcasting And Upcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Downcasting And Upcasting" Watch "Downcasting And Upcasting" New topic
Author

Downcasting And Upcasting

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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 ??


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Simone Aiello
Greenhorn

Joined: Oct 13, 2010
Posts: 22

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?!


The important is not what you know, but when you know it...
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
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.)


OCPJP 6-100%; Preparing for GATE11
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Casting plays major role during polymorphism. Polymorhically you will send sub classes to super classes and downcast to your sub-class.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
See Furst
Greenhorn

Joined: Aug 04, 2010
Posts: 29
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?


Doin' Java to be one of the cool kids.
I usually use Perl;
 
 
subject: Downcasting And Upcasting