Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

about casting/downcasting

upcast downcast

  • Please log in to reply
4 replies to this topic

#1 John Jang

John Jang

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 May 2011 - 05:32 PM

I read a really good post about up and downcasting on this forum:
http://forum.codecal...owncasting.html

But my question is, why do you want to cast?
(referring to the link I posted up there)
if all cats can perform animal methods, why would you want to upcast a cat to an animal in first place?

p.s: wasn't sure where to post...
  • 0

#2 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 15 May 2011 - 08:56 PM

Found a really good tutorial on this. I don't know this so I'm currently reading it.
http://forum.codecal...owncasting.html
  • 0

#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 16 May 2011 - 03:24 AM

You want to cast because you will have to write less code. Take the example of Cat and Dog who extend Animal. Now you have another class Cage (how cruel ^^). Cage can hold both dog and cat. Without casting your Cage class will look like:
public class Cage{
  private Cat cat;
  private Dog dog;

  public void addCat(Cat cat){
     this.cat = cat;
  }

  public void addDog(Dog dog){
    this.dog = dog;
  }
}
While with casting you can just do

public class Cage{
  private Animal animal;
  
  public void addAnimal(Animal animal){
    this.animal = animal;
  }
}

Also with generics, like an ArrayList, you can't put a Dog AND a cat in an ArrayList.
ArrayList<Dog, Cat> myPets = new ArrayList<>(); //Won't work
That's not possible. You can however decide to put Animal in the arrayList, and then both dog and cat can be added:
ArrayList<Animal> myPets = new ArrayList<>();
myPets.add(new Cat());
myPets.add(new Dog());
...

Cat cat = (Cat) myPets.get(0);
Dog dog = (Dog) myPets.get(1);

  • 1

#4 Petros

Petros

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts
  • Location:United States
  • Programming Language:Java, (Visual) Basic
  • Learning:Java

Posted 11 January 2013 - 09:59 AM

Genus! Here is a master piece that complements perfect the Cat and Dog example! A practical example. Great job. I always say, the simple the better! I'll expend ours and days looking for someone that can explain just the reason for casting! If there is Polymorphism why casting! Thanks, thanks, thank you very much, finally my nightmares have come to an end. From today on I won't have to spend any more time trying to find a reason for casting. I'm a free maaaaaaan!
  • 0
Java Java Doooooo!!!

#5 Petros

Petros

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts
  • Location:United States
  • Programming Language:Java, (Visual) Basic
  • Learning:Java

Posted 23 January 2013 - 02:24 PM

I figurit out!! Since ClassD extends ClassC and ClassC extends ClassB, ClassD was being capture by the ClassB evaluation code. So when I placed the condition upsidedown (evaluating D, C, B [backwards]), it works perfect! Thanks any way. It was a good exercise on casting!

Edited by Petros, 24 January 2013 - 05:41 AM.

  • 0
Java Java Doooooo!!!





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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download