This discussion is archived
4 Replies Latest reply: Sep 27, 2006 8:28 AM by 807598 RSS

Casting a subclass into a superclass?

807598 Newbie
Currently Being Moderated
So, I have this exercise that demonstrates abstraction and interfaces. I wanted to explore it a little more. Let me start by showing the code:
public class TestAnimals {
     public static void main (String [] args){
          Fish f = new Fish();
          Cat c = new Cat("Fluffy");
          Animal a = new Fish();
          Animal e = new Spider();
          Pet p = new Cat();
          Animal [] pets = new Animal[5];
          
          pets[0] = new Cat("Tiger");
          pets[1] = new Cat("Mama");
          pets[2] = new Spider();
          pets[3] = new Fish("Flipper");
          pets[4] = new Fish("Jaws");
          
          //demonstrate different implementations of an interface
          f.play();
          c.play();
          
          //demonstrate virtual method invocation
          e.eat();
          e.walk();
          
          //demonstrate calling super methods
          a.walk();
          
          //demonstrate calling super methods
          for (int i=0;i<pets.length; i++){
               System.out.println("------------");
               pets.walk();
               pets[i].eat();
               if (pets[i] instanceof Cat){
                    pets[i].play();
               }
          }
     }
}


this won't compile, because play() is not a method of Animal, it's a method of subclass Cat. Having an array of animals, and interating through the array, I want it to make the Animal play() if it is a Cat. How do I do that?
  • 1. Re: Casting a subclass into a superclass?
    Herko_ter_Horst Newbie
    Currently Being Moderated
    You need to cast the object to Cat after you have determined that it is in fact an instanceof Cat:
    ((Cat)pets).play();Note that in general using instanceof in this way is an indication of badly designed code. 
  • 2. Re: Casting a subclass into a superclass?
    807598 Newbie
    Currently Being Moderated
    Ok, thanks. I did try to cast it, but I did it like this:
    pets[i] =(Cat)pets;
    pets[i].play();


    Which didn't work, and I don't understand why.

    Also, you noted that calling a subclass method from a superclass is typically indicitive of bad design, but how would I do this properly? I mean, if I wanted to have an Array of different types of Animals, but wanted the animals to do different things depending on what they are, why wouldn't I want to do it this way? I can't create a Fish in the Cat class, so I can't use a Cat[], and so the only way to have an array with a Fish and a Cat is to make an Animal[].

    Can you tell me what would be correct?

    Sorry in advance if this is too simple of a question for this forum. I am currently going through the Java programming course WJ-2753, and am not sure where else I can ask these questions...
  • 3. Re: Casting a subclass into a superclass?
    807598 Newbie
    Currently Being Moderated
    Ok, thanks. I did try to cast it, but I did it like
    this:
    pets[i] =(Cat)pets;
    pets[i].play();


    Which didn't work, and I don't understand why.
    That doesn't work because the pets array is declared to be an array of Animal, so even if you cast one of them to a Cat, as soon as you put it back in the array it turns into an Animal again. Do it the way Herko_ter_Horst   suggested and it will work.
    Also, you noted that calling a subclass method from a
    superclass is typically indicitive of bad design, but
    how would I do this properly? I mean, if I wanted to
    have an Array of different types of Animals, but
    wanted the animals to do different things depending
    on what they are, why wouldn't I want to do it this
    way? I can't create a Fish in the Cat class, so I
    can't use a Cat[], and so the only way to have an
    array with a Fish and a Cat is to make an Animal[].

    Can you tell me what would be correct?
    If each type of animal has one action it can perform that is different for each animal, then declare an abstract method performAction in the Animal class and implement in differently for each type of animal. e.g. for the cat you might do it as:
    public void performAction()
    {
    play();
    }
    That way you can call performAction on any Animal without casting and it will perform the correct action for each type of animal.

    Sorry in advance if this is too simple of a question
    for this forum. I am currently going through the Java
    programming course WJ-2753, and am not sure where
    else I can ask these questions...
    No question is too simple. As long as it's asked politely and clearly and you're not expecting someone to do all your work for you.
  • 4. Re: Casting a subclass into a superclass?
    807598 Newbie
    Currently Being Moderated
    Wonderful replies, thank you all very much.