开发者

Java Polymorphism problem

I've learned all these programming terms in Swedish so please bear with me..

I'm having problems calling a method in a subclass which should override a method in the superclass.

Here is the class structure with removed code:

public interface Movable {
    public void move(double delta);
}
public abstract class Unit implements Movable, Viewable{
    public void move(double delta){
            System.out.println("1");
    }

}
public class Alien extends Unit{
    public void move(long delta){
        System.out.println("2");
    }
}
public class Player extends Unit{
    public void move(long delta){
        System.out.println("3");
    }
}

public void main(){
    ArrayList<Unit> units = new ArrayList<Unit>();
    Unit player = new Player();
    Unit alien = new Alien();
    units.add(player);
    units.add(alien);
    for (int i = 0; i < this.units.size(); i++) {
        Unit u = (开发者_如何学运维Unit) this.units.get(i);
        u.move();
    }
}

This would output 1 and 1, but I want it to output 2 and 3.

What am I doing wrong? I thought this was how it worked.


public class Alien extends Unit{
    public void move(long delta){
        System.out.println("2");
    }
}
public class Player extends Unit{
    public void move(long delta){
        System.out.println("3");
    }
}

Here's your problem, in the interface you have move declared as:

public void move(double delta)

But your child classes declare a:

public void move(long delta)

That creates a new function, separate from move and doesn't override it. To fix it change Alien's and Player's move functions to:

public void move(double delta)

Also, as Outlaw Programmer points out, you can have the compiler catch this sort of error by adding an @Override annotation right above the declaration of any function you intend to override a function in the parent class. Like so:

public class Alien extends Unit {
    @Override
    public void move(double delta) {
        System.out.println("2");
    }
}


Your subclasses (Alien and Player) aren't overriding the move() method in their parent class because you have declared 'delta' as a long and not a double.

You can have the compiler spot some of these errors by using the @Override annotation.


You need to add the @Override flag to the move function in your alien and player classes.

that would've helped you notice that you are not quite overriding your move since the type in alien/player are long, not doubles


Your child class does not implements the same method signature:

public void move(double delta);

is not the same as:

public void move(long delta);

To catch this kind of error at compile time, you can add @Override in the method signature. The compiler will check if the child class do in fact override a method in the parent class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜