-1

I was learning about custom ArrayAdapter.Found this project on Github. I can't figure out why super is used here.

public AndroidFlavorAdapter(Activity context, ArrayList<AndroidFlavor>Flavors) {

        super(context, 0, Flavors);
  }

When I remove super this error pops up.

X There is no applicable constructor to '()’

Any help?

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
fetive
  • 17
  • 2
  • On the other side, @fetive, I would recommend do your research first before posting here, that way you would learn better and everyone will benefit. – omt66 Jul 28 '20 at 02:54
  • i tried to find out but didn't understand there explanation.:( – fetive Jul 28 '20 at 02:56
  • @omt66 can you please tell me why the second parameter in super is 0? – fetive Jul 28 '20 at 03:09
  • 1
    Does this answer your question? [super() in Java](https://stackoverflow.com/questions/3767365/super-in-java) – Amir Dora. Jul 28 '20 at 03:26

2 Answers2

3

Every constructor needs to another constructor as the first thing it does1. There are three ways to do this:

  • A constructor can make an explicit super call, either with or without parameters. The parameters types need to match the signature of a constructor declared in the superclass.

  • A constructor can make an explicit this call. This calls another constructor declared by this class.

  • If there no explicit super or this class, an implicit super() call is added to the constructor by the Java compiler. For this to work, there needs to be a constructor in the superclass with no arguments; i.e. a no-args constructor.

1 - Except for java.lang.Object which has no superclass. Note that the bytecode verifier checks this. If you use (say) a bytecode assembler to create a class with a constructor that doesn't call a superclass constructor, it will be rejected by the classloader.


So ...

Why super(...) is used in Constructor?

To explicitly call a superclass constructor. Notice that in this case you are passing arguments to the superclass constructor.

When I remove super this error pops up: "There is no applicable constructor to '()’"

That is because the compiler cannot find the superclasses no-args constructor that is implicitly invoked if you don't have an explicit super(...) call.

Can you please tell me why the second parameter in super is 0.

The javadocs for the superclass should explain what that means. In this case, the 2nd parameter is a Resource ID. I'm not sure that it makes sense, but I've seen it said that Resource ID 0 means null.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Because the base class is probably doing the required initialization for the instance.

omt66
  • 4,765
  • 1
  • 23
  • 21