• 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

Upcasting and downcasting: is this code and my understanding of it right?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Psydeo code// I forgot how to spell that word!
public class Pizza {...}
public class DeepDish extends Pizza {...}
public class Neopolitian extends Pizza {...}
Pizza p;
DeepDish onions = new DeepDish();
Neopolitian works = new Neopolitian();
p= onions; // This is upcasting
onions=( DeepDish) p; // This is downcasting and
// this must be explicit

//////////////////////////////

with p= onions;
This makes a general object reference ( superclass)
and replaces it with a object reference of the subclass.
This is called upcasting

for this code:
onions =( DeepDish) p;
This makes the superclass reference replace the subclass.
///////////////////////////////////////
Can anyone else tell me if this is right/correct?
and how would you explain it?
Is upcasting and downcasting a type/version of polymorphism?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pseudo that is how you spell it now i know!
I know overloading/overriding is polymorphism:
So I am thinking upcasting and downcasting is a form
of polymorphism because the objects are replacing other objects.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Cyberoptic
(you do have a way with words)
polymorphism means many forms. This is indded polymorphism as a single object is able to take the form of any of its parent classes.
regards
sanjeev
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. U r right. And this is a version of polymorphism.
That is coz if you call a method that is written in the superclass and overridden in the subclass, the actual method that gets executed depends on the type of the object. To explain,
take the foll case:
DeepDish onions=new DeepDish();
Pizza p=onions;
//(p is a superclass object reference referring to an object of
//the subclass).
p.someCommonMethod();
//(assuming it is defined in both Pizza and DeepDish)
Here, the subclass's version of the someCommonMethod() will be executed. If p is made to refer to a Pizza object and the p.someCommonMethod() statement is executed, then the superclass's version is executed.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So,
superclass = subclass object // upcasting
Object = MyClass //
MyClass = AnotherClass // Wrong!
childclass = parentclass // requires a cast!
A subclass ref. is assigned to a superclass reference
this makes the subclass object can be used where the
superclass can be used. Thus a subclass is replacing
a superclass object.
Is upcasting a widening conversion?
///////////////////////////////
subclass object ( subclass ) superclass // downcasting
A superclass is replacing a subclass.
The value of the superclasses reference is cast, to a
subclass .
collection object "Hashmap1" = ( collection class "Hashmap") object // object being the superclass of all classes
Downcasting is valided at compile time and checked at runtime.
Can this be called a narrowing conversion ?
////////////////////////////////////
Overloading = resolved at compile time.
So is upcasting resolved at compile time?
And would that mean upcasting is a form of overloading?
Overriding is resolved at runtime.
So is downcasting resolved at runtime right?
And would that mean downcasting is a form of overriding?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone have more examples? I always liked lots of examples
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The terms "upcasting" and "downcasting" are C++ terms, not Java terms. They do not occur in the JLS. But having said that, I have to add that if they were used in Java, then you would be using them right (:-). That is, your understanding seems to be approximately correct.
You further asked somewhere about whether they could be considered widening/narrowing conversions. The answer is yes: the conversions that you are calling upcasting/downcasting are described in the JLS as widening/narrowing conversions.
Here is the citation:
5.5 Casting Conversion
... Casting contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), a narrowing primitive conversion (�5.1.3), a widening reference conversion (�5.1.4) [this is your upcasting], or a narrowing reference conversion (�5.1.5) [your downcasting].
A bit later you wrote the following about what you call upcasting:
"A subclass ref. is assigned to a superclass reference this makes the subclass object can be used where the superclass can be used.
Thus a subclass is replacing a superclass object. "
You are headed in the right direction, but I have some problems with putting it this way.
Say that Boat is a subclass of Vehicle, and we write:
Vehicle v;
Boat b = new Boat();
v = b;
Now in the assignment "v = b", nothing is being replaced. (Of course the old value of v is being replaced with a new one but I don't think that's what you were referring to.)
What is happening is that a **value** is being put into a kind of value-container called a **variable**. We know (at compile time) that the value, at runtime, will certainly be an instance of Boat, or some subclass of Boat, perhaps a Dory. And we declared V as a Vehicle, which means you can put a reference to any vehicle into it. Since Boat is a subclass of Vehicle, anything that is a Boat is also a Vehicle, so this is legal.
But there is nothing being replaced and nothing being changed, except which vehicle v currently points at.
So much for upcasting; now let's consider downcasting, with the same class hierarchy but the code:
Boat b;
Vehicle v;
<code which sets v to something legal>
b = (Boat)v;
Here we defined a variable b that can hold any value that is an instance of a Boat. We assign to it the current value of v. The compiler knows that the value of v must be a vehicle but not every vehicle is a boat. so you have to say, explicitly, that you intend to do this, and accept the risk of a runtime error if the value of v turns out to be, say, a wheelbarrow.
Again, nothing is replaced (except the old value of b) and nothing is changed. The object itself does not change its type. It just goes into a different location, where a different amount of knowledge is known at compile time, but the real reality is runtime, and this particular value (let's say it's the Dory "Hattie S"), is always the same thing, and the same type, wherever it gets put.
[This message has been edited by Jim Goodwin (edited September 15, 2000).]
[This message has been edited by Jim Goodwin (edited September 15, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the big reply. Anyway almost every java book that i have except for my awt and swing books. Have upcasting and downcasting in the index. ( Just Java 2, Mughal's A programmers guide to Java certification, Deitel the Java 2 complete training course, Bill Brodgen and www.bruceeckel.com )
What is the " java term" for upcasting and downcasting?
functions we call the methods, no parent or child classes but
superclasses and subclasses etc...

PS: Can I use your post Jim Goodwin for my upcoming tutorial
on polymorphism, is a, has a, is like a etc...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic