Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification in chapter 5 about constructor rules (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In page 243 of the OCA Sybex guide

point 5 about constructor rules says If the parent doesn't have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor

but is there a requirement to place a call to the parent constructor in each child constructor? code below compiles fine and I haven't placed a call to the super constructor in line 12. should i understand it as place an explicit call to super, if there is no this() call in the child constructor , as either super() or this() can be the first statements in the constructor.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:but is there a requirement to place a call to the parent constructor in each child constructor? code below compiles fine and I haven't placed a call to the super constructor in line 12. should i understand it as place an explicit call to super, if there is no this() call in the child constructor.

As you (should) know: the first statement of any constructor has to be a call to a superclass constructor (using super()) or a constructor in the same class (using this()). If you don't provide an explicit call to a constructor, the compiler automatically adds a call to the superclass no-arg constructor for you, the compiler will add super(); as the first statement of the constructor. But if the superclass doesn't have a no-arg constructor, the compiler will complain and give you a compiler error. So you should add an explicit call to a superclass constructor in every child constructor which doesn't already have a call to another constructor of the child class (using this()). That's why your code snippet compiles successfully: the no-arg constructor of the Mychild class has an explicit call to another constructor of the Mychild class and therefore doesn't need an explicit call to a superclass constructor (as you can only have a call to this() or a call to super() as the first statement of any constructor). And the 1-arg constructor of the Mychild class has an explicit call to the superclass 1-arg constructor. If you would remove line18, the Mychild class won't compile.

So point 5 should probably be rephrased a little bit to avoid confusion. It might be more accurate if it was changed to "If the parent doesn't have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor which doesn't have an explicit call to another constructor within the child (using this())" (or something similar).

Hope it helps!
Kind regards,
Roel
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the book actually covers this pretty well, but to make sure you understand it, can you see why only one of the following four ChildClass's will compile?



Because ParentClass does not have a no-argument constructor, any attempt use the default no-argument constructor in a child class fails, therefore only ChildClass4 compiles. From a compiler's perspective ChildClass1/ChildClass2/ChildClass3 are actually all equivalent, as the compiler will convert them all to an equivalent form of ChildClass3.
 
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
Scott Selikoff wrote:I think the book actually covers this pretty well

I agree! The 5 points combined have indeed very good coverage of this topic. But if you really want to nitpick then the phrase "in each child constructor" (in the 5th point) is not completely accurate as you only need an explicit super() call if the child constructor doesn't already have an explicit call to another child constructor (using this()). But this conclusion can be made as well based if you combine the 1st and 2nd point (of the constructor rules).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic