...
Consider two classes A and B:
Code Block | ||||
---|---|---|---|---|
| ||||
public class A{ public A(){ // lots of constructor activity. } } |
Code Block | ||||
---|---|---|---|---|
| ||||
public class B extends A{ public B(){} } |
...
Does the constructor for A get called when a B is constructed?
Or do I need to add the following line to get A constructed?
Code Block | ||||
---|---|---|---|---|
| ||||
public B{ super(); } |
Answer
...
- If a java class has no explicit constructor it is assumed to have an "implicit" empty public no-arg constructor:
Code Block title java MyClass.java public MyClass(){}
- If a constructor does not explicitly call either the super constructor or another of its own constructors (using
this(args)
) it is assumed to have an "implicit" call tosuper()
as the first line of the constructor. Note also thatthis()
orsuper()
can only be called as the first line of a constructor.
...