0

Possible Duplicate:
Why does this() and super() have to be the first statement in a constructor?

I just learned that at school, but the teacher doesn't know why.

I can think of some good reasons, but I think there are cases when the initializing can be done later in the constructor- before you use the variables form the mother class, for example. OK, the variables should be initialized from the start, but that's not always necessary.

I"m guessing there are a more reasons for that why must super() be placed in the first line of the constructor.

So, why must I write super() in the first line of the constructor, when I'm inheriting a class?

Community
  • 1
  • 1
AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34
  • 2
    "not always necessary"; but can you think of a reason why you shouldn't? – Viruzzo Jan 19 '12 at 09:48
  • I meant this: you can declare a variable or object one line before you use it. So why can't this be done with super()? Awoodland gave me a really good answer. – AlexSavAlexandrov Jan 19 '12 at 14:04

2 Answers2

5

The class you're inheriting from needs to be able to complete its construction, before you have start working on your own class.

Without doing this you could do lots of "bad" things, e.g.

  1. Pass this to another method somewhere else, which uses the base class, before its constructor has run. That would break lots of assumptions

  2. Call polymorphic functions that haven't been "set up" correctly yet. As well as anything done by the class itself the implementation possibly uses the constructor call to handle implementation internals too.

    Accessing an object before it's been constructed is bad, in the same way that fried chicken "is-a" chicken you really don't want to access (eat) that chicken before it's been fried.

  3. Access protected/public member variables of the base class which the base class was trying to promise would always be initialised to some state.

It's perfectly reasonable for a class to make a promise that any instances of it will always be in some given state. If you get a chance to do things before the constructor has been called then there's no way to honour promises like that. Essentially the "is-a" relationship wouldn't actually hold if the thing that it "is" isn't actually that thing yet!

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Now that explains a lot. Thanks! – AlexSavAlexandrov Jan 19 '12 at 14:05
  • as per @quaylar's answer (which should really be a comment because it is incomplete in answering the question), writing `super()` at the start of a constructor where the superclass has no constructor arguments is just noise and is unnecessary. Can you add this to your answer? – Dave Moten Feb 13 '20 at 00:15
1

In addition to awoodlands answer: You dont have to write super(), since the Java-Compiler will automatically call all the default constructors up the hierarchy.

Exception: If you dont have a default constructor in the base class, you would have to call your custom constructor using super(ConstructorArgs args).

quaylar
  • 2,617
  • 1
  • 17
  • 31