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

Constructors and parent constructors

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm slightly confused by a question in the practice exercises in the OCA book.
In chapter 5, page 291 question 2 there is the following code:

My answer was wrong because I said it would compile but apparently because the parent class Mammal doesn't define a no-argument constructor it doesn't compile.
So, a few observations first please.
I recreated this in Eclipse and in fact I do get an error due to the constructor so I had to amend the Platypus class to be

and that works. Now what I don't get is the fact though that in Platypus we create a new Mammal object, so we're invoking the Mammal constructor directly without the Platypus one ever being called I would have thought. So much so that the output of that code is "Mammal" only. In the solutions of the exercises though, they say that with the right constructor added we should get instead "PlatypusMammal" as output, but that doesn't seem to be the case, why is that?
Also, on a more general note, is it safe to say that when we have a parent class with a constrcutor which isn't a no-argument constructor the child class will ALWAYS have to call super(xxx)?
thanks
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the output would be MammalPlatypus and not PlatypusMammal as indicated
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Now what I don't get is the fact though that in Platypus we create a new Mammal object, so we're invoking the Mammal constructor directly without the Platypus one ever being called I would have thought. So much so that the output of that code is "Mammal" only. In the solutions of the exercises though, they say that with the right constructor added we should get instead "PlatypusMammal" as output, but that doesn't seem to be the case, why is that?

I think maybe they meant if they called new Platypus() instead of new Mammal(5). Otherwise your observation is correct.

Also, on a more general note, is it safe to say that when we have a parent class with a constrcutor which isn't a no-argument constructor the child class will ALWAYS have to call super(xxx)?

Yes.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello jason,
whenever you will declare a constructor of subclass you will have to declare a superclass constructor (which you have not done in your former code)
because suppose if any variable rely on your super class for initialization that can only be achieved via calling super constructor.if your superclass class constructor is a no-arg constructor then compiler will impicitly call it in your subclass constructor thats what happened in your latter code.
hope this helps!

kind regards,
praveen.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, I understand the rule now.
Still, the solution of the exercise seems wrong.
I think maybe they meant if they called new Platypus() instead of new Mammal(5).

Probably, but unfortunately that's not what they're saying and this really confused me because for a moment - well untill I tried the code myself - I thought that the Platypus constructor would be called even if we're creating a new Mammal object.
Here is the response in full:
The code will not compile because the parent class Mammal doesn't define a no-argument constructor, so the first line of a Platypus constructor should be an explicit call to If there was such a call, then the output would be MammalPlatypus since the super constructor is executed before the child constructor

So they're effectively saying that with the current code and the amendment the output would be MammalPlatypus but it will be instead only Mammal. Not sure if you guys think this should be in the errata, but I've found it very confusing and inaccurate.
cheers
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:

 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:I thought that the Platypus constructor would be called even if we're creating a new Mammal object.

No. Constructors are only called if you perform the new operator on the constructor's enclosing class, or any of its sub-classes.

So they're effectively saying that with the current code and the amendment the output would be MammalPlatypus but it will be instead only Mammal.

Yes, I agree. They probably meant if the Platypus constructor was called, but they didn't write that clearly.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Also, on a more general note, is it safe to say that when we have a parent class with a constrcutor which isn't a no-argument constructor the child class will ALWAYS have to call super(xxx)? 
Yes, because If a constructor body of a class does not begin with an explicit constructor invocation like using super("Java") Or super(10) etc then the constructor body implicitly begins with a superclass constructor invocation super() i.e. an invocation of the constructor of its direct superclass that takes no arguments.

Jason Attin wrote: Not sure if you guys think this should be in the errata
Your correct, It is in errata list, please have a look OCAJP 8 Errata list
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your correct, It is in errata list, please have a look OCAJP 8 Errata list

Yes no, that specific problem I mentioned isn't there. What's there is an entry that refers to something different, the wrong line in the answer (should be line 7 rather than line 8). The issue with the result is another one and not mentioned in the errata anywhere. Well, anyway doesn't matter, I just thought that since the explanation says what would happen if the code was correct we might as well make sure that the output given is correct and currently it isn't.
 
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
Jason Attin wrote:The issue with the result is another one and not mentioned in the errata anywhere.

I disagree! You should have looked more carefully in the errata Both issues were reported in this topic (and added to the errata overview). Please note that the explanation of the mock question is on a different page (346) than the wrong line number in the mock question itself (291).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic