• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the use of Upcasting?  RSS feed

 
Raga Jana
Ranch Hand
Posts: 35
Google Web Toolkit Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May be the question is look like silly. But really i'm not able to understand why i have to use upcast??

In the below example:i found by googled..



Please make me clear!! And i need explanation on the following syntax..



When i assign the reference of the LB_2 to LB_1.. Where the lb3 reference use to point??
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mayilsamy Annamalai wrote:May be the question is look like silly.

There a no silly questions. Only silly answers.
Mayilsamy Annamalai wrote:But really i'm not able to understand why i have to use upcast??

You don't.
Mayilsamy Annamalai wrote:In the below example:i found by googled..

And which website was that?

Mayilsamy Annamalai wrote:Please make me clear!! And i need explanation on the following syntax..

The lb3 is a reference to the LB_2 object.


 
Raga Jana
Ranch Hand
Posts: 35
Google Web Toolkit Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is the URL: http://www.janeg.ca/scjp/overload/poly.html

And could you explain me the concept of upcasting?? Why we have to do that??

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have given a reference to the wrong page on that website. When you find the notes complete as a pdf, it says
If you cast up the class hierarchy you do not have to use the cast operator; if you are cast down the class hierarchy you must use the cast operator
It looks almost as though you had misread the original text.

Beware: that .pdf is dated 2002, so it is at least two major versions out of date.
 
Raga Jana
Ranch Hand
Posts: 35
Google Web Toolkit Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.. The provided link is right only.. And you to download the example code!! Anyway thanks for your reply.. But still there is no answer for my question.. What's the purpose to use the Upcasting??
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost never need to up-cast. It it only useful for hiding implementation details but I would rather use "Programming against an interface" for that. Try to create a example using Animal, Dog and Cat. Maybe then it will be more clear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!