• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Default Constructor Always Called?

 
Ranch Hand
Posts: 172
  • 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.
 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE 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!
 
Ranch Hand
Posts: 472
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.
 
Greenhorn
Posts: 2
Oracle MySQL Database Java
  • 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: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic