0

I want to extend ArrayAdapter but call

ArrayAdapter.createFromResource(context,textArrayResId,textViewResId);

instead of

super(context,textArrayResId,textViewResId)

inside myArrayAdapter constructor, but I'm always getting a there is no default constructor available in android.widget.ArrayAdapter error.

It looks like that having to call super is obligatory, so when I use it instead, the code compiles fine. Is what I ask to do possible ?

public class myArrayAdapter<CharSequence> extends ArrayAdapter {

    public myArrayAdapter(Context context, int textArrayResId, int textViewResId) {
       // super(context,textArrayResId,textViewResId);
        ArrayAdapter.createFromResource(context,textArrayResId,textViewResId);

    }
}
microwth
  • 1,016
  • 1
  • 14
  • 27
  • well, it either invokes the `super()` implicity if you don´t explicity call a super constructor and if there is a default constructor aviable, or you need to specify the super constructor you´d like to call. That´s part of polymorphism.. – SomeJavaGuy Oct 11 '16 at 11:08
  • 2
    What are you trying to *achieve* by calling `ArrayAdapter.createFromResource`? It's not obvious what the aim is here. – Jon Skeet Oct 11 '16 at 11:09
  • 1
    No, it is not.The super-class must be initialized for the sub-class to be initialized. If no default constructor exists, you have to call a parameterized constructor. – Turing85 Oct 11 '16 at 11:09

3 Answers3

2

Parent class constructor must be called explicitly in this scenario. You may create static factory method for this purpose.

What are static factory methods?

Community
  • 1
  • 1
1

The problem is that when you delete that line the compiler tries to create the super object using default constructor and since in your case your super class doesnt have any default constructor this wont work.

You are extending an object while you are not going to let it to be created !!!

You cant do this. You must call the super constructor in order for that to create an instance.

In your case you can delete extend ArrayAdapter So that it gets kinda a factory class that creates an object of ArrayAdapter type for you ;)

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
1

From the JLS, § 8.8.7:

The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass [...] If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

This means that a superclass-constructor must be called. The implicit call always refers to super(). If this constructor does not exist, one has to call a superclass constructor explicitly.

To answer your question: you have to call a constructor explicitly. Calling the static method does not help. You could, however, look at the code of createFromResource(...), which should somewhere end in a constructor call of ArrayAdapter.

Turing85
  • 18,217
  • 7
  • 33
  • 58