-1

I implemented an expandedGridView like this Kishanjvaghela's github example or the answer of Raj008 in stack Overflow.

It works fine, but I wonder why does it need to call super() in the constructor

public class ExpandableHeightGridView extends GridView {
    boolean expanded = false;

    public ExpandableHeightGridView(Context context) {
        super(context);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
/* more code here */
}

If there is no constructor, java must call parent constructor, isn't it ?

Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29
europrimus
  • 53
  • 5
  • Check this [Why is constructor of super class invoked when we declare the object of sub class?](https://stackoverflow.com/q/7173019/7666442) and this one [Why do this() and super() have to be the first statement in a constructor?](https://stackoverflow.com/a/6318640/7666442) – AskNilesh Jul 08 '19 at 08:15
  • extending a class, you have to declare at least one constructor calling parent class constructor. Only if a parent class has the default constructor (with no arguments) you can skip declaring constructor. – Vladyslav Matviienko Jul 08 '19 at 08:25

1 Answers1

0

Because by default(optional) super() keyword will call no-arg constructor, So to call argumented constructor of parent class it must to write super() keyword with arguments

just see how constructor chaining works

Rahul
  • 1,380
  • 1
  • 10
  • 24