Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Constructor's call

 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, this is my first post in this forum, by the way, forgive me for my english (it is not my native language). Today I was doing an exercice for the OCPJP exam, and the mock had this question:



From CertPal - www.certpal.com



Before this question, I used to think that, if I call an overloaded constructor this will call it's parent constructor. But analysing this question, it come to me that, to call the constructor of the parent I need to call the non-argument constructor of the child.

That's correct?

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

Matheus Souza wrote:Hey guys, this is my first post in this forum, by the way, forgive me for my english (it is not my native language). Today I was doing an exercice for the OCPJP exam, and the mock had this question:





is implicitly doing this:


Therefore, if you do this:

The System.out.println("deveil") in the Devil() constructor will be called.

 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:

Matheus Souza wrote:Hey guys, this is my first post in this forum, by the way, forgive me for my english (it is not my native language). Today I was doing an exercice for the OCPJP exam, and the mock had this question:





is implicitly doing this:


Therefore, if you do this:

The System.out.println("deveil") in the Devil() constructor will be called.



But Helen, this implicity call of super() doesn't have to be with arguments (super(eye)) to the overloaded constructor in Devil?

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't specify your own super(eye) call, the compiler will insert the non-argument invocation for you, which will not invoke a constructor that does take arguments.
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Pouwels wrote:If you don't specify your own super(eye) call, the compiler will insert the non-argument invocation for you, which will not invoke a constructor that does take arguments.


I got that, but my point is: the overloaded constructor with an argument, before running it calls the constructor with no arguments, which calls the parent's constructor?
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matheus Souza wrote:

Jim Pouwels wrote:If you don't specify your own super(eye) call, the compiler will insert the non-argument invocation for you, which will not invoke a constructor that does take arguments.


I got that, but my point is: the overloaded constructor with an argument, before running it calls the constructor with no arguments, which calls the parent's constructor?



No, when you don't specify a call to a super constructor, the compiler will insert a SUPER invocation. So the no-argument constructor of the same class won't run. In order to do that, you will need to invoke this().
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Pouwels wrote:

Matheus Souza wrote:

Jim Pouwels wrote:If you don't specify your own super(eye) call, the compiler will insert the non-argument invocation for you, which will not invoke a constructor that does take arguments.


I got that, but my point is: the overloaded constructor with an argument, before running it calls the constructor with no arguments, which calls the parent's constructor?



No, when you don't specify a call to a super constructor, the compiler will insert a SUPER invocation. So the no-argument constructor of the same class won't run. In order to do that, you will need to invoke this().



I got it. Thanks folks =D
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic