• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is 'Upcasting' called so?  RSS feed

 
Yelena Druzhilova
Greenhorn
Posts: 4
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

could you please explain, why is "Upcasting" called so?
For example, I have 2 classes:



and the following code:



where I get "Camera start...." message from the Camera class.

For me it's more logically, if with so name, I'll get "Machine start" message in such case.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags, which you should always use, and you can see how much better the post looks

That is not casting of any kind. That is the use of polymorphism. I shall try searching for a good description.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For descriptions of polymorphism, try clicking the word polymorphism, which I think gives you a better description than the Java Tutlroials.
 
Yelena Druzhilova
Greenhorn
Posts: 4
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for code tags and the explanation, Campbell Ritchie.

Is it correct to say that upcasting in this example is

?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yelena Druzhilova wrote:could you please explain, why is "Upcasting" called so?

Because you are casting UP the hierarchy. In general this is not required, since any subclass can be assigned directly to a superclass, as you already proved with your:
Machine m = new Camera();
statement.
You may see the term (or something like it) used with 'covariant' return values though (Google it).

And you must cast whenever you downcast, viz:
Camera c = (Camera) someObject;
and you run the risk that the code may throw a ClassCastException at runtime if someObject is not, in fact, a Camera.

HIH

Winston
 
Yelena Druzhilova
Greenhorn
Posts: 4
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Winston and Campbell.
I've understood the difference between polymorphism and casting, and what does 'upcasting' mean.

Thank you for you time.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yelena Druzhilova wrote:Thank you for code tags and the explanation, Campbell Ritchie.

Is it correct to say that upcasting in this example is

?
No, it isn't. You are not casting anything. You are using a declared type which differs from the runtime type. Did you read the link about Animals and Dogs?

And “you're welcome
 
Yelena Druzhilova
Greenhorn
Posts: 4
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did but I can't catch the 'Upcasting' meaning exactly.
I've spent a lot of time trying to find the right explanation of this term and found the following definitions in different resources and on the coderanch:

1. Upcasting: Java permits an object of a sub class can be referred by its super class. It is done automatically.

2. Calling a method from a parent class. (If so, is it probably calling a method from the base class without polymorphism?).
I.e. if I remove start() method from Camera class and run the code:

If these definitions aren't correct could you, please, help with a sample or reference to the Upcasting explanation?
 
Ulf Lindqvist
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yelena Druzhilova wrote:
If these definitions aren't correct could you, please, help with a sample or reference to the Upcasting explanation?


The term upcasting is a common generic term but it's not defined by Java. In the JLS it's called,

---

5.1.5. Widening Reference Conversion

A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.

Widening reference conversions never require a special action at run time and therefore never throw an exception at run time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.

---

But let's call the above upcasting. This is an example of upcasting indeed,



and this is too,



and this,



In the last case the upcast has been made explicit. This is not necessary.

Machine is a supertype of Camera and that's what makes all above assignments upcasts. Upcasting takes place upward in an inheritance hierearchy.

The deeper reason why upcasting works is polymorphism, namely that an object may have many types (polymorphism == many forms). A subtype object has its own type but also at the same time all types of its supertypes. It IS all of them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!