Reputation: 28773
I have a class that will be needed to create derived classes with similar functionality.
public abstract class PreloaderAdapter<T> extends BaseAdapter {
private final List<T> mList;
...
public void addItems(List<T> list) {
mList.addAll(list);
notifyDataSetChanged();
}
public void clear(boolean isRepaint) {
mList.clear();
if (isRepaint) {
notifyDataSetChanged();
}
}
...
}
In derived classes I can override those methods but don't want, as they are the same.
Can you, please, say me, will it be enough to write so?
public class SomeListAdapter extends PreloaderAdapter<MyClass> {
private final List<MyClass> mList;
...
public void addItems(List<MyClass> list) {
super.addItems(list);
// mList.addAll(list);
// notifyDataSetChanged();
}
public void clear(boolean isRepaint) {
super.clear(isRepaint);
// mList.clear();
// if (isRepaint) {
// notifyDataSetChanged();
// }
}
...
}
Or should I uncomment these lines and delete them in a superclass?
Should I make a superclass as abstract?
Should I use fields inside a superclass?
Upvotes: 0
Views: 85
Reputation: 111
In Java, if a method is declared and implemented at a superclass, if you extend this superclass, the subclass will have that method implemented by default.
e.g
public class Dog {
//Atributes
//Methods
public void bark() {...}//Do an action
}
public class PitBull extends Dog {
//Atributes
//Methods
public void action2() {...}//Do another action
}
With this code, you can do this:
PitBull dogObject = new PitBull();
dogObject.bark();
Upvotes: 1
Reputation: 300
Upvotes: 1