Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

OCP11 Ch: Class Design - How to call super() if parent class doesn't have a no-argument constructor?

 
Ranch Hand
Posts: 32
6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the OCJP Complete Study Guide Ch - 8: Class Design in the Reviewing Constructor Rules section, point 4 says

If the parent class doesn’t have a no-argument constructor, then every constructor in the child class must start with an explicit this() or super() constructor call.



My confusion is, what if the parent class just has a parameterized constructor? In that case, Java wouldn't provide the default non-parameterized constructor and then, if I invoke super() from the child class, wouldn't that be referring to something that doesn't exist?

Or am I over-thinking this and the book refers to a parent class that doesn't have any constructor at all, in which case calling super() from a child class would correctly invoke the default non-parameterized constructor provided by Java?
 
Bartender
Posts: 3699
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

You cannot call super(); if parent class has no-args constructor (implicit or explicit).

Might this code example be helpful?

 
Vimugdh Lall
Ranch Hand
Posts: 32
6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Mikalai. I was trying to say the same thing. Which is why the line of the book that I quoted in the first post is confusing regarding the no-args constructor. As long as there is a parameterized constructor present, I must always call that constructor and not the super() constructor in the child class, even if the parent class doesn't have a no-args constructor.
 
Mikalai Zaikin
Bartender
Posts: 3699
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vimugdh Lall wrote:You're right Mikalai. I was trying to say the same thing. Which is why the line of the book that I quoted in the first post is confusing regarding the no-args constructor. As long as there is a parameterized constructor present, I must always call that constructor and not the super() constructor in the child class, even if the parent class doesn't have a no-args constructor.



The authors assumed a different idea, IMHO.

When they said: "must start with an explicit this() or super()" they mean "must start with an explicit this(), this(...) or super(...)"
They wanted to emphasize (again, IMHO) that the developer now needs explicit call, and they (IMHO) did not mean literally super(), but super(XXX);
 
Vimugdh Lall
Ranch Hand
Posts: 32
6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:

Vimugdh Lall wrote:You're right Mikalai. I was trying to say the same thing. Which is why the line of the book that I quoted in the first post is confusing regarding the no-args constructor. As long as there is a parameterized constructor present, I must always call that constructor and not the super() constructor in the child class, even if the parent class doesn't have a no-args constructor.



The authors assumed a different idea, IMHO.

When they said: "must start with an explicit this() or super()" they mean "must start with an explicit this(), this(...) or super(...)"
They wanted to emphasize (again, IMHO) that the developer now needs explicit call, and they (IMHO) did not mean literally super(), but super(XXX);



Yupp! I just completed the Review Questions of the chapter and realized that. I came here to post this exact thing but you beat me to it lol! Cheers mate!  
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic