Search code examples
javaandroidinheritanceparameterized-constructor

when is it obligatory to define constructors in subclasses?


trying to inherit array adapter class it gives me an error when not defining a constructor in subclass....why should I define a constructor

public class WordAdapter extends ArrayAdapter<word> {
    public WordAdapter(Activity context, ArrayList<word> word) {

        super(context,0, word);
    }

Solution

  • Question : ...why should I define a constructor?

    Answer: Since ArrayAdapter class has no default constructor, we need to define a constructor that matches one of the six constructors in the superclass.

    You can read more about this here:

    https://medium.com/@Sudhagar/android-array-adapters-what-most-of-the-tutorials-don-t-tell-you-90f898fb54a2

    Not every constructor is being inherited by a subclass. ArrayAdapter has no default constructor and that is the reason you need to define a constructor which should be called.

    There are lots of articles which describe the exact behavior of constructors in subclasses.

    When is it obligatory to define constructors in subclasses?

    Answer: In this case if no default constructor is present in the superclass.