• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sybex CSG page 324 Constructor Rule #5 Errata?

 
Ranch Hand
Posts: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the parent class doesn’t have a no-argument constructor and the child doesn’t define any constructors, then the child class will not compile.





More precise?

If the parent class doesn’t have a no-argument constructor but has at least one non-no-argument constructor, and the child doesn’t define any constructors, then the child class will not compile.


 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.  The second statement adds complexity but no new value, because...

The term "Default Constructor" is used solely for the one that you get for free from the compiler [that does nothing but call super()  with no arguments] if and only if you declare no constructors for a class.  [It is legacy behavior carried over from C++ that had to be C compatible, some people think it is stupid and annoying, but will always be the case, forever.]

Both the "Default Constructor" and a "No-Args Constructor" you define yourself are equally "No-Args" constructors if they exist.
Of course, one you write can be filled with more meaningful default values for your class members, but they are both "No-Args" constructors.

The exams love to trip you up and catch you out on all sorts of combinations of things about this.

One simple case is the first of the two quotes you posted.

Another is that child/sub-class constructors must begin with a legitimate call to super() that matches a direct super-class constructor which is available to it --
unless they begin with a this(...) call to another constructor from their own class which makes a legitimate call to super(...) if they do not, an invisible call to super() with no args gets inserted by the compiler.

These things are easier to remember and to use than to say, in my opinion.  The key thing is that a sub-class constructor always begins with an implict call to super() with no-args unless it begins with a different call to super(...) or to another of its own constructors which does.

Everything else comes from that.
 
Charles O'Leary
Ranch Hand
Posts: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:No.  The second statement adds complexity but no new value


Hi Jesse,

I could be wrong, but I was thinking the value IS clarity and/or completeness, especially since the quote does not include the term "default constructor" and I think I provided a concrete example to the contrary of the quoted constructor rule?   Upon first read, I'm not the only one that potentially walked away with a different view ... please look here for example.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol, I found the same thing confusing, and I hope I didn't make things worse by saying super(...) which is a syntax that is used literally for VarArgs in Java [also on the exam].

Many references refer to calls to this() and super() meaning this(with, some, parameters, or, maybe, none) and super( also, with, some, parameters, or, maybe, none)

That confused me a bit sometimes.

I am a bit OCD, in that if we "call the DBMS a database, what do we call a database?" things always bother me.

1. Always reserve the term "default constructor" for the one you get IFF you define no others.
2. If you sub-class a class without a "No-Args" constructor, you must always make an explicit call to some valid super(   something-in-here ) reference.  Some places say super() but they still mean that (which I don't like).

Let's compare the statements:

If the parent class doesn’t have a no-argument constructor and the child doesn’t define any constructors, then the child class will not compile.


This is true as written for all cases.  Some people 'round these parts hate the terms parent and child classes, but that's preference.

If the parent class doesn’t have a no-argument constructor but has at least one non-no-argument constructor, and the child doesn’t define any constructors, then the child class will not compile.


When I said this doesn't add value I meant that we could have said:
If the parent class doesn’t have a no-argument constructor but has a silly-sounding name, and the child doesn’t define any constructors, then the child class will not compile.

And it would be equally true, as would:
If the parent class doesn’t have a no-argument constructor and it is a three-day weekend, and the child doesn’t define any constructors, then the child class will not compile.

The no-argument constructor that you get due to the fact that you didn't write any constructors is the only default constructor.  Official Java documentation maintains this usage thru-out.
Other no-argument constructors are not "default constructors", tho I could see someone wanting to call them that because if you subclass the class, and don't make an explicit call to super( something ) you will implicitly call that one.

I used the term "default constructor" to mean any no-arguments constructor often and sometimes annoyed people, but it didn't matter as I wasn't taking any certification exams.
I used to apologize if they seemed steamed, but thought they were OCD.

This gets to be a stupid logic thing where you have a liar and someone who always tells the truth guarding two doors, one leads to treasure, the other to death.
You get to ask one question of one guard and then have to go in, what question do you ask?

If you have no no-args constructor, than it already tells us that you have at least one not no-args constructor, otherwise you would have been granted one implicitly by the compiler.
There actually are questions on the exam that revolve around this, so it is important to remember.

I wasn't trying to be snarky, I too used the lingo imprecisely for a long time, and it could cause you to get questions wrong...so now I am a stickler.
 
Ranch Hand
Posts: 32
6
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles O'Leary wrote:

If the parent class doesn’t have a no-argument constructor and the child doesn’t define any constructors, then the child class will not compile.





More precise?


If the parent class doesn’t have a no-argument constructor but has at least one non-no-argument constructor, and the child doesn’t define any constructors, then the child class will not compile.




You're right Charles. I had made a post about this a while back asking the same thing https://coderanch.com/t/738615. The wording in the book is definitely confusing.
 
Charles O'Leary
Ranch Hand
Posts: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do acknowledge:

First, There is an implicit/default no-arg constructor for both the Parent and Child classes, so maybe my example actually serves to prove the point of the rule.  At first glance, I was misguided by looking for an explicit constructor per the quoted rule.
Second, I did get the overall point: the constructors absolutely must match (per rules).
Finally, expressing ALL of this in words so that it's crystal clear ... I'll gladly leave this to Scott & Jeanne.  

Jesse,

Jesse Silverman wrote:I wasn't trying to be snarky


I didn't take it that way.  Thanks for replying!
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough.  I complained about the wording of many things in both Jeanne and Scott's books and the one by the Enthuware guys.

In this case, I had people grunting and groaning at me every time I called a non-default no-args constructor a default constructor years ago (back in C++, before Java) that this one never bothered me.

Remember, final variables are NOT EFFECTIVELY FINAL....that one killed me, when I went back and looked at the text, it had said that several different times.
 
Charles O'Leary
Ranch Hand
Posts: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I complained about the wording


Agreed..."word problems" are (always) more error prone when compared to "code problems" ... I think that was my take from another post somewhere ... and likely what I think may be going on here, in my head at least:

WARNING:  I was trying to move on from this, but I think I can point to some confusion points for me, and more importantly potentially other readers, at first glance ... so my rhetorical "ramblings" continue:

It is stated that the child doesn't define any constructors: default no-arg constructor? "any constructors" to me includes both implicit and explicit?  Is the default constructor considered a "defined" constructor?

If the parent class doesn’t have a no-argument constructor:  same points and this doesn't mean other constructors are or are not present

As mentioned, I get the overall point, but this rule was clearly written to be one of many golden rules to be applied very quickly within a 90 minute exam and there's a lot here

To my earlier point, Kudos for the code examples and other text in the book that break everything down.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say if a class doesn't have code for a constructor it doesn't define one. (So only explicit ones).
reply
    Bookmark Topic Watch Topic
  • New Topic