Chris Lalande
Chris Lalande

Reputation: 213

Android Activity with Abstract Base Class throws a compile error "is not abstract and does not overrid abstract method..."

I have a base class that extends ListActivity, that I want to use to implement a lot of common code used on multiple different Activity implementations.

I want to make this base class abstract to enforce implementation of a method on the subclasses.

If I write a base implementation of the method, and override it in the subclasses, everything works fine.

If I make the method abstract, I get the following compile error:

XXXX.subapps.athletics.AthleticsNewsActivity is not abstract and does not override abstract method getListItems() in XXXX.subapps.BaseCategoryListActivity

Base Class:

public abstract class BaseCategoryListActivity extends ListActivity
{
    ....
    abstract ArrayList<String> getListItems();
    ....
}

Example subclass:

public class AthleticsNewsActivity extends BaseCategoryListActivity
{
    ....
    public ArrayList<String> getListItems()
    {
       ...implementation details....
    }
}

Upvotes: 2

Views: 1480

Answers (3)

mtraut
mtraut

Reputation: 4740

The visibility does not match, make both public.

Out of topic: Prefer using interfaces (List) not implementations (ArrayList) in the signature.

Upvotes: 1

JonH
JonH

Reputation: 33163

Change access modifier to public abstract

You have it listed just as:

abstract ArrayList<String> getListItems();

Change it to read:

public abstract ArrayList<String> getListItems()

and provide an implementation for it.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240928

if you extend an Abstract Class you need to make the method either abstract in extended class or you need to provide implementations:

public abstract class BaseCategoryListActivity extends ListActivity
{
    ....
    public abstract ArrayList<String> getListItems();
    ....
}

public class AthleticsNewsActivity extends BaseCategoryListActivity
{
    ....
    public ArrayList<String> getListItems()
    {
       ...implementation details....
    }
}

This should work, note the change of public modifier
overridden method can't have broader visibility specifier

Also See

Upvotes: 3

Related Questions