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
开发者_StackOverflow中文版
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....
}
}
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
- Docs
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.
The visibility does not match, make both public.
Out of topic: Prefer using interfaces (List) not implementations (ArrayList) in the signature.
精彩评论