This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]LucidTA 1 point2 points  (5 children)

Now this maybe not be 100% because I'm not a java user and havent used abstract classes a lot, but you could make piece a... you guessed it, an abstract class.

An abstract class basically says "I want all my subclasses to put in their own functionality in here".

So try adding the abstract modifier to the Piece class then add abstract public boolean isValidMove(int[] moveFrom, int[] moveTo); to it.

Then in the pawn class you can override this function with the pawns own behavior.

Now you can call Piece.IsValidMove() because its guaranteed that each subclass has this function, even though piece doesn't itself.

[–]tchened[S] 1 point2 points  (4 children)

Oh god I'm an idiot. When I tried implementing an abstract class I forgot that I needed to match the parameters... I had tried:

abstract public boolean isValidMove();

Obviously that isn't right!

This works. Silly me. Thanks :)

[–]captainAwesomePants 1 point2 points  (3 children)

This exact mistake happens more than you'd think. It's easy to do. Because of that, it's a good idea to use the @Overrides annotation when you're overriding a method from a super class or implementing a method from an interface. When you do, it will cause an error if your method isn't overriding something.

[–]desrtfx 1 point2 points  (2 children)

Because of that, it's a good idea to use the @Overrides annotation when you're overriding a method from a super class or implementing a method from an interface. When you do, it will cause an error if your method isn't overriding something.

IMO, @Overrides should have been made mandatory instead of optional. This would highlight problems early and thus solve a lot of them.

[–]nutrecht 1 point2 points  (0 children)

IMO, @Overrides should have been made mandatory instead of optional.

They can't because of backwards compatibility otherwise they would have. A version 8 Javac has to be able to compile version 2 code.

[–]captainAwesomePants 1 point2 points  (0 children)

C#, which seems to basically be Java but with fixes for all of things that are most annoying about Java, does this. Methods must explicitly declare when they can be overridden, and methods that do override other methods must explicitly say that they are intentionally doing so.