Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
* * * * * 2 votes

Upcasting, downcasting

upcast downcast

  • Please log in to reply
17 replies to this topic

#1 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 11 September 2009 - 07:35 AM

*
POPULAR

Upcasting and downcasting are important part of Java, which allow us to build complicated programs using simple syntax, and gives us great advantages, like Polymorphism or grouping different objects. Java permits an object of a subclass type to be treated as an object of any superclass type. This is called upcasting. Upcasting is done automatically, while downcasting must be manually done by the programmer, and i'm going to give my best to explain why is that so.

Upcasting and downcasting are NOT like casting primitives from one to other, and i believe that's what causes a lot of confusion, when programmer starts to learn casting objects.

Throughout this tutorial i'm going to use Animal hierarchy to explain how class hierarchy works.

Inheritance

images.jpg

What we have here, is a simplified version of an Animal Hierarchy. You can see, that Cat and Dog are both Mammals, which extends from Animal, which silently extends from Object. By silently, i mean, that Java automatically extends every class from Object class, which isn't extended from something else, so everything is an Object (except primitives).

Now, if you ask - is Cat an Object - It doesn't extend Object, it extends Mammal?
By inheritance Cat gets all the properties its ancestors have. Object is Cat's grandgrandparent, which means Cat is also an Object. Cat is also an Animal and a Mammal, which logically means - if Mammals possess mammary glands and Animals are living beings, then Cat also has mammary glands and is living being.

 

Basic JTable and Netbeans

What this means for a programmer, is that we don't need to write for every possible Animal, that it has health. We just need to write it once, and every Animal gets it through inheritance.
Consider the following example:
 

class Animal {
int health = 100;
}

class Mammal extends Animal { }

class Cat extends Mammal { }

class Dog extends Mammal { }

public class Test {
public static void main(String[] args) {
Cat c = new Cat();
System.out.println(c.health);
Dog d = new Dog();
System.out.println(d.health);
}
}

When running the Test class, it will print "100" and "100" to the console, because both, Cat and Dog inherited the "health" from Animal class.

Upcasting and downcasting

First, you must understand, that by casting you are not actually changing the object itself, you are just labeling it differently.
For example, if you create a Cat and upcast it to Animal, then the object doesn't stop from being a Cat. It's still a Cat, but it's just treated as any other Animal and it's Cat properties are hidden until it's downcasted to a Cat again.
Let's look at object's code before and after upcasting:

Cat c = new Cat();
System.out.println(c);
Mammal m = c; // upcasting
System.out.println(m);

/*
This printed:
Cat@a90653
Cat@a90653
*/

As you can see, Cat is still exactly the same Cat after upcasting, it didn't change to a Mammal, it's just being labeled Mammal right now. This is allowed, because Cat is a Mammal.

Note that, even though they are both Mammals, Cat cannot be cast to a Dog. Following picture might make it a bit more clear.

 

download.jpg

Although there's no need to for programmer to upcast manually, it's allowed to do.
Consider the following example:
 

Mammal m = (Mammal)new Cat();

is equal to
 

Mammal m = new Cat();

But downcasting must always be done manually:
 

Cat c1 = new Cat();
Animal a = c1; //automatic upcasting to Animal
Cat c2 = (Cat) a; //manual downcasting back to a Cat

Why is that so, that upcasting is automatical, but downcasting must be manual? Well, you see, upcasting can never fail. But if you have a group of different Animals and want to downcast them all to a Cat, then there's a chance, that some of these Animals are actually Dogs, and process fails, by throwing ClassCastException.

 

Double buffering, movement, and collision detection

This is where is should introduce an useful feature called "instanceof", which tests if an object is instance of some Class.
Consider the following example:
 

Cat c1 = new Cat();
Animal a = c1; //upcasting to Animal
if(a instanceof Cat){ // testing if the Animal is a Cat
System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");
Cat c2 = (Cat)a;
}

Note, that casting can't always be done in both ways. If you are creating a Mammal, by calling "new Mammal()", you a creating a Object that is a Mammal, but it cannot be downcasted to Dog or Cat, because it's neither of them.
For example:

Mammal m = new Mammal();
Cat c = (Cat)m;

Such code passes compiling, but throws "java.lang.ClassCastException: Mammal cannot be cast to Cat" exception during running, because im trying to cast a Mammal, which is not a Cat, to a Cat.

General idea behind casting, is that, which object is which. You should ask, is Cat a Mammal? Yes, it is - that means, it can be cast.
Is Mammal a Cat? No it isn't - it cannot be cast.
Is Cat a Dog? No, it cannot be cast.
Important: Do not confuse variables with instances here. Cat from Mammal Variable can be cast to a Cat, but Mammal from Mammal variable cannot be cast to a Cat.

 

Creating an Executable Jar File

Cats cant purr, while being labeled something else
If you upcast an object, it will lose all it's properties, which were inherited from below it's current position. For example, if you cast a Cat to an Animal, it will lose properties inherited from Mammal and Cat. Note, that data will not be lost, you just can't use it, until you downcast the object to the right level.
Why is it like that? If you have a group of Animals, then you can't be sure which ones can meow() and which ones can bark(). That is why you can't make Animal do things, that are only specific for Dogs or Cats.

 

dont know.jpg

However the problem above is not an obstacle, if you choose to use polymorphism. Polymorphism uses automatic downcast during method calls. I'm not going to go into details with this one, so i'm referring to Polymorphism tutorial by Turk4n: http://forum.codecal...lymorphism.html
updown5.png

Upcasting during method calling

The beauty of casting is that programmer can make general methods, which can take a lot of different classes as an argument.
For example:
 

public static void stroke(Animal a){
System.out.println("you stroke the "+a);
}

This method can have what ever Animal or it's subclass as an argument. For example calling:
 

Cat c = new Cat();
Dog d = new Dog();
stroke(c); // automatic upcast to an Animal
stroke(d); // automatic upcast to an Animal

..is a correct code.

updown3.png

however, if you have a Cat, that is currently being held by Animal variable, then this variable cannot be argument for a method, that expects only Cats, even though we currently have a instance of Cat - manual downcasting must be done before that.

updown4.png

About variables

Variables can hold instance of objects that are equal or are hierarchically below them. For example Cat c; can hold instances of Cat and anything that is extended from a Cat. Animal can hold Animal, Mammal, etc..
Remember, that instances will always be upcasted to the variable level.

"I really need to make a Dog out of my Cat!"

Well, you can't do it by casting. However, objects are nothing else, but few methods and fields. That means, you can make a new dog out of your Cat's data.

Let's say you have a Cat class:
 

class Cat extends Mammal {
Color furColor;
int numberOfLives;
int speed;
int balance;
int kittens = 0;

Cat(Color f, int n, int s, int B){
this.furColor = f;
this.numberOfLives = n;
this.speed = s;
this.balance = b;
}
}

and a Dog class.
 

class Dog extends Mammal {
Color furColor;
int speed;
int barkVolume;
int puppies = 0;

Dog(Color f, int n, int s, int B){
this.furColor = f;
this.speed = s;
this.barkVolume = b;
}
}

and you want to make a Dog out of the Cat. All you need to do, is, place a method inside of the Cat class, that converts the fields and returns a new Dog based on that.
 

public Dog toDog(int barkVolume){
Dog d = new Dog(furColor, speed, barkVolume);
d.puppies = kittens;
return d;
}

As you can see, they don't match that well, so some fields were inconvertible, and some data had to be made from scratch. Notice, that numberOfLives and Balance were not converted, and barkVolume was completely new data. If you have 2 Classes, that match perfectly, then hurray, but it rarely happens.
conversion can now be called from where ever you need:
 

Cat c = new Cat(Color.black, 9, 20, 40);
Dog d = c.toDog(50);

Thanks for reading.

 

Looking for some more related topics?

About casting/downcasting

Polymorphism help ..!!

Inherritance or Interfaces

 

 

Attached Images

  • arrayf.png
  • pointer.png
  • upcast downcast.jpg

Edited by Roger, 02 May 2014 - 08:56 AM.
some pictures were gone.

  • 11

#2 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,088 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 September 2009 - 08:32 AM

I like the images. Very nice! +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 ThemePark

ThemePark

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 11 September 2009 - 11:35 AM

I could have really used this a few days ago! Also, I get the feeling that this is made because of my thread. Like WingedPanther said, nice images and nice, understandable tutorial which cleared up a few things for me.

+rep
  • 0

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 11 September 2009 - 11:38 AM

Wow, nice work! The images really are nice. +rep
  • 0

#5 debtboy

debtboy

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 499 posts

Posted 11 September 2009 - 05:22 PM

Awesome, and I'm not a even a Java guy +rep
  • 0

#6 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 13 September 2009 - 12:53 AM

Also, I get the feeling that this is made because of my thread.


You are quite right. That's where i got the idea. : )
Thanks, everybody.
  • 0

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1,263 posts

Posted 13 September 2009 - 01:16 AM

A lot of work went in to this, really high quality! Great tutorial!

+rep :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 ssabrewolf

ssabrewolf

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 November 2009 - 09:46 PM

Jajaa very creative, thank you for all the effort you invested on it , i was looking for an automated downcasting way, i mean, if i have an ancestor with say n=100 propertys and its subclasses has n+m propertys , is there any mean to initialize the first n propertys from an already constructed superclass object? one posible solution its throug dynamic call to java methods, but this is prone to errors and design holes in a production enviroment
  • 0

#9 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2,764 posts

Posted 17 November 2009 - 03:52 AM

well done.
you deserve more +rep :D
i like the images, really neat
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#10 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 17 November 2009 - 05:18 AM

is there any mean to initialize the first n propertys from an already constructed superclass object?


I don't think that's possible. I think you need some conversion method, which initializes all the fields. But if you find a shortcut, then let me know :)
  • 0

#11 kailas

kailas

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 12 February 2010 - 12:33 PM

thanks for the clear tutorial
  • 0

#12 shanakaf

shanakaf

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 June 2010 - 09:19 AM

Really nice lesson.. Now I understood the concept.. thnx a lot :) cheers
  • 0





Also tagged with one or more of these keywords: upcast, downcast