c0der
c0der

Reputation: 73

Polymorphism issue in Java

I have a polymorphous array and I need to fix the bottom two lines of code. I'm having trouble understanding Polymorphism, and I'm trying to figure out it's logic.

In this code I'm assuming Animal = superclass, Dog = subclass, Cat = subclass:

Animal[] animals = new Animal[20];
animals[0] = new Dog();
animals[1] = new Cat();

//these two below don't work
Dog lassie = animals[0];
Cat fluffy = animals[1];

Upvotes: 2

Views: 88

Answers (4)

Ryan J
Ryan J

Reputation: 8323

It won't work because the type of the array is Animal, which means the objects in the array are of type Animal. You're assigning an individual element which is either a Dog or Cat to a reference of that particular animal, but the object is of type Animal, even though the actual element is a Dog or a Cat. The compiler doesn't know what kind of Animal it is, without a cast.

For that statement to work, you need a cast to the appropriate type.

Dog lassie = (Dog) animals[0];
Cat fluffy = (Cat) animals[1];

Polymorphism though, isn't really used in this manner, since the idea is that you as the user, don't need to know the specific type of Animal, just that it's an Animal.

Suppose your Animal class defined an abstract method called speak() and your subclasses both implemented it, System.out.println("Woof") for a Dog and System.out.println("Meow") for a Cat, to keep it simple.

Example:

public abstract class Animal {
   public abstract void speak();
}

public class Dog extends Animal {
   @Override
   public void speak() {
       System.out.println("Woof!");
   }
}

public class Cat extends Animal {
   @Override
   public void speak() {
       System.out.println("Meow!");
   }
}

If you then had your array as such:

Animal[] animals = new Animal[20];
animals[0] = new Dog();
animals[1] = new Cat();

for (Animal a : animals) {
    a.speak();  // you don't know what Animal it is, but the proper speak method will be called.
}

Upvotes: 3

Dair
Dair

Reputation: 16240

The static type is Animal for animal[0] and animal[1]. So basically you're telling java:

Let this Dog lassie be an animal. And let this Cat fluffy be an animal. But not all animals are necessarily cats or dogs. See why Java might get upset with this?

You need to reassure Java using a caste:

Dog lassie = (Dog) animals[0];
Cat fluffy = (Cat) animals[1];

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

You're trying to assign a type of Animal to Dog (assuming Dog inherits from Animal), the assignment can't work, as the compiler can only guarantee that the object is of a type of Animal, it could be a Horse for all it knows

But you can cast Animal to Dog or Cat again, assuming that Dog and Cat inherit from Animal

Dog lassie = (Dog)animals[0];
Cat fluffy = (Cat)animals[1];

Having said that, I would be very careful about blind casting values in this way.

When defining an array as Animal, you are basically saying, I don't care about the contents, so long as it meets the contractual requirements of the class/interface Animal, beyond that, you shouldn't care

Upvotes: 1

amahfouz
amahfouz

Reputation: 2398

That is not how use polymorphism. The idea is that once you added elements to a polymorphic collection, you should treat them all as just Animal. If they have different behavior, then this goes into polymorphic methods that are defined on the superclass and implemented in the subclass. Let me know if this is not clear.

Upvotes: 1

Related Questions