Question

I've written this code and as part of the requirements, I must use polymorphism. I was wondering if this would be considered to be polymorphic?

runAnimation is a method in the Animation class that prints the image onto the screen, this code is in a different class altogether.

Thanks in advance.

Animation dragon1;
Animation dragon2;


dragon1 = new Animation(5, tex.dragon1[0], tex.dragon1[1], tex.dragon1[2], tex.dragon1[3]);
dragon2 = new Animation(5, tex.dragon2[0], tex.dragon2[1], tex.dragon2[2], tex.dragon2[3]);

dragon1.runAnimation();
dragon2.runAnimation();
Was it helpful?

Solution

No.

Polymorphism, in Java, refers to subclasses of a class which share many of the same methods (defined in the parent), but which implement (override) a method that makes it unique to the subclass. You have just made two objects from the same class (Animation).

You might want to search for polymorphism in Java (here is a response from the language tutorial).

Here is a crude example:

public class Mammal {
    private String name;
    public Mammal(String aName) { name = aName; }
    public String getName() { return name;}
    public int getLegs() {return 4; }
    public int getHands() {return 0;}
}

public class FourFootedMammel extends Mammal {
    public FourFootedMammel(String aName) {super(aName);}
}

public class TwoFootedMammal extends Mammal {
    public TwoFootedMammal(String aName) {super(aName); }
    public int getHands() {return 2;}
    public int getLegs() {return 2; }
}   

TwoFootedMammal human = new TwoFootedMammal("Human");
FourFootedMammal dog = new FourFootedMannal("Dog");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top