Question

I have an interface that I want to implement that looks like this, but with more methods. There is just one here for example:

public interface List{

public void add(int position, Album album);

}

Then my class that implements it starts like this, including all the methods present within the interface:

public class SongList implements List{

public void add(int position, Album album){
...my code for this method
}

My compiler tells me two errors. The first - when compiling my List interface - says that the interface List cannot find my Song class:

List.java:23: error: cannot find symbol
public void add(int position, Song item);
                            ^
symbol:   class Song
location: interface List
1 error

I have a song class that compiles and is in the same folder as the interface and all of my other java files.
Second - when compiling SongList - the compiler says that I haven't overridden the add() method:

SongList is not abstract and does not override abstract method add(int,Object) in List
public class SongList implements List{
       ^
1 error

I'm pretty lost here...as far as I have googled I am following all of the rules for interfaces, but apparently not. Any ideas what I'm doing wrong?

Was it helpful?

Solution

The first and second code blocks indicate that the add method is expecting an Album, but the third and fourth code blocks indicate that you've implemented an add method with a Song parameter. Either the interface needs an add method that expects a Song, or else your class needs to implement an add method that expects an Album - the methods need the same parameters.

OTHER TIPS

I think that you are importing the wrong List interface.

Please check that your List interface is mentioned in the import statements and no other List interface are imported as the error statement says..

add(int,Object)

and not

add(int,Album)

I think that you are accidentally implementing the java list interface.

I hope this is helpful..

For your first problem, clearly the compiler is having trouble locating Song.class. If Song.java is in the same directory, ensure that it has been compiled.

For the second problem, implementing an interface means you have to implement each of it's methods. That means you need to write a method with the exact same method signature - which includes the parameters.

Though this time, since you didn't include all the methods in your interface, I'll just assume they're all implemented. In which case it looks like R.daneel.olivaw is correct, you might be using the wrong List. Ensure the signatures match, and that your version of List is being used. As mentioned above, that means it has to be compiled and located on java's classpath (or in the same directory).

Edit: Originally misinterpreted the second part of the question, this answer has been updated accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top