The method *****is undefined for the type***** in Java

I'm trying to call a method from an abstract class Sprite in another package, but I got "The method getSymbol() is undefined for the type Sprite"

Here's the code.

And here is the code from another package sprites

I guess the problem is that the method from an abstract class cannot be instantiated.

But I don't know how to fix it.

Jon Skeet
people
quotationmark

This is the problem:

public class ArrayGrid<Sprite> implements Grid<Sprite>

The way you've declared the class, Sprite is the name of a type parameter. You've made this a generic class, and I suspect you didn't mean to. Within that class, Sprite refers to the type parameter, not the type - so you could have an ArrayGrid<String> which implemented Grid<String>... at which point you'd have a string array rather than a sprite array, so it's no wonder that getSymbol() wouldn't work, just as one symptom of the problem.

I suspect you just wanted:

public class ArrayGrid implements Grid<Sprite>

At that point, Sprite really refers to the type. And that means you can avoid the code that wouldn't work around arrays, and instead just write:

this.grid = new Sprite[numRows][numColumns];

Then there's no need to suppress the warnings :)

people

See more on this question at Stackoverflow