• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Constructor Always Called?

 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! From Enthuware:


What can be done to make this code compile?

Answer is :
Make Bird constructor public: public Bird() { ... }

1) Am I right to say that the superclass's no-args constructor is always called before the the rest of the code in the subclass's constructor is executed, whether or not the subclass constructors are no-args or with-args?

2) Am I right to say that the compiler will insert a super() call to the superclass constructor even if there is a with-args constructor in the superclass?

Please advise. Thanks.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could easily verify this by adding some System.out.println(...) statements in all of your constructors above!
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you not put your constructor in class (any constructor with args or not), then will be inserted default constructor which is looks like

also if you not put to user defined constructor call to this or super, call to super(); with no args will be inserted
also pay some note if you have abstract class it has constructor too. and if you create sub class of abstract class it (constructor of abstract class) be invoked too.
interface has no constructor.
 
Ed Burgess
Greenhorn
Posts: 2
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Yes, unless you specify a super constructor with args on the first line of the subclass constructor (or you can specify the no-args constructor yourself on the first line of the subclass constructor).

2) Yes, if there is a no-args super constructor, the compiler will insert a call to it. If there is not, and you do not specify an appropriate with-args super constructor, the code will not compile.
 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Blass wrote:1) Am I right to say that the superclass's no-args constructor is always called before the the rest of the code in the subclass's constructor is executed, whether or not the subclass constructors are no-args or with-args?

It depends! A superclass constructor will always be executed before the subclass constructor. But it's not always a no-arg constructor. It depends (of course) on which constructors are defined in the superclass. Another nice to know: you can't use instance members (fields and methods) until the super constructor has finished.

Alan Blass wrote:2) Am I right to say that the compiler will insert a super() call to the superclass constructor even if there is a with-args constructor in the superclass?

It depends! If you explicitly added a call to another constructor of the subclass (using this) or a call to a superclass constructor (using super), then the compiler won't add a super() call. Otherwise a super() call will be inserted by the compiler. If the superclass doesn't define a no-arg constructor, your code will not compile (of course).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic