Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "class must implement abstract method X", but it already does

I have this abstract superclass:

public abstract class MediaListActivity extends Activity {
    ...
    abstract void launchMedia(MediaFile mediaFile);
}

and the concrete subclass:

public class VideoListActivity extends MediaListActivity {
    ...
    void launchMedia(MediaFile mediaFile) {
        // TODO: implement launchMedia()
    }
}

At the first line of the VideoListActivity subclass, Android Studio gives the error

Class 'VideoListActivity' must either be declared abstract or implement abstract method 'launchMedia(MediaFile)' in 'MediaListActivity'

I can see why it should. But it does! Doesn't it? I've checked that the signature matches; I've tried Option+Return to get the IDE to suggest fixes; nothing works.

Is this just a mistake on Android Studio's part? or am I doing something wrong?

Now it happens that the classes are in different packages: the subclass VideoListActivity is in a "sub-package," foo.bar.baz.video, whereas the superclass MediaListActivity is in package foo.bar.baz.

If I move VideoListActivity up into foo.bar.baz package, the error goes away. But I have several of these concrete subclasses, and I don't want to clutter the top-level package with them: I like them organized into their subpackages. What can I do to make this error go away (assuming it's even necessary)?

like image 605
LarsH Avatar asked Oct 20 '22 05:10

LarsH


1 Answers

The answer is to make sure the implementation of the method launchMedia() is accessible outside its package: i.e. it must be protected or public. I thought I had tried that already, but I might have been looking at the wrong method. Anyway, that makes the error go away.

like image 98
LarsH Avatar answered Oct 27 '22 11:10

LarsH