• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Call to an overloaded constructor

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 322 of OCP Java SE 11 Programmer 1.  Rule number one states:

1. The first statement of every constructor is a call to an overloaded constructor via this(), or a direct parent constructor via super().

The first part of this sentence doesn't make sense to me. In what situation would a constructor call an overloaded constructor?  It would make sense to me that say that an overloaded constructor's first statement would be a call to a no-arg constructor, i.e. this().  But, that is not what is written.

Do I misunderstand this all together? Can someone help clarify what I'm misreading about that rule?
 
author & internet detective
Posts: 39996
807
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have the following code. The first constructors calls the overloaded second constructor. Does that help?

 
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the confusion is between a literal interpretation of this() (a no-argument constructor) and a general reference to this() (any overloaded version of the constructor).

Same thing applies to the reference to super(): is that a literal reference to a no-argument constructor of the superclass or a general reference to any constructor of the superclass?
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please call the superclass the superclass or supertype or base type, not parent. Never mind how many people say parent, it isn't an accurate term.
Remember that the complier imputes a super(); without arguments to every constructor without super(...); or this(...); written by the programmer. That calls the no‑arguments constructor in the immediate superclass. Remember that the compiler will add a default constructor if you don't write any constructors. You can write super(); if you wish, but you don't have to. You do however have to write super(something); to match a superclass constructor if you wrote any superclass constructors and they all take arguments. Remember, since super(); and this(); with or without arguments have to be the first statement in the constructor, you can't write both.In those examples each this(...); or super(...); call matches a different constructor. Why do you want to write this(...); and super(...);? To avoid the sort of code following, with repeated code and awkward setXXX() method calls. In fact, too many setXXX() methods are often poor design.Isn't that last lot of code horrible. It also means you have to take the private modifier away from the validateXXX() methods.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brad Ellis wrote:On page 322 of OCP Java SE 11 Programmer 1.  Rule number one states:

1. The first statement of every constructor is a call to an overloaded constructor via this(), or a direct parent constructor via super().

The first part of this sentence doesn't make sense to me. In what situation would a constructor call an overloaded constructor?  It would make sense to me that say that an overloaded constructor's first statement would be a call to a no-arg constructor, i.e. this().  But, that is not what is written.

Do I misunderstand this all together? Can someone help clarify what I'm misreading about that rule?



I think that with 'overloaded' the intend is to avoid a recursive call, forcing to call an overloaded version of the current constructor. For example:



the word 'overloaded' here means that you could call another version of the constructor


Another point is that 'this()' refers to 'the use of the implicit pointer this to call a constructor'.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic