I was following a tutorial on creating a custom ArrayAdapter for a ListView in Android. Here is the custom adapter class from the tutorial:
public class NumbersViewAdapter extends ArrayAdapter<NumbersView> {
// invoke the suitable constructor of the ArrayAdapter class
public NumbersViewAdapter(@NonNull Context context, ArrayList<NumbersView> arrayList) {
// pass the context and arrayList for the super
// constructor of the ArrayAdapter class
super(context, 0, arrayList);
}
How does the argument pass in doesn't have to match the constructor in the super class? To be more clear, I thought the argument pass in:
NumbersViewAdapter(@NonNull Context context, ArrayList<NumbersView> arrayList)
has to match with:
super(context, 0, arrayList)
second of all, I saw no constructor has the argument pass in as such (@NonNull Context context, ArrayList<NumbersView> arrayList)
. The only thing I think is close with it is
public ContactListAdapter(@NonNull Context context, int resource, @NonNull ContactInfo[] objects) {
super(context, resource, objects);
}
I haven't try out anything yet
super()
is injected by the compiler), either with values it received from its caller, values it derived, or some fixed values. There is no requirement for a constructor of a subclass to be identical to its superclass (otherwise all classes would only have a no-arg constructor, because all classes are ultimately a subclass ofObject
.