• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification about code snippets on Page 241 (Java OCA 8 Programmer I Study Guide)

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On page 241 I see below example:-



Looks like this code is getting compiled.


The first example on page 242 is getting compiled as well:



Could you please clarify if I am doing something wrong?

Thanks, Raghu
 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is some issue in the order I created the java class. Please disregard this issue.
 
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
Raghavendra Desoju wrote:On page 241 I see below example:

Looks like this code is getting compiled.

The study guide is spot-on! The code snippet on page 241 does not compile. Because no constructor was defined in the Elephant class, the compiler will (try to) add a default no-arg constructor with a call to super(). So the compiler tries to add this code to the Elephant classAnd line1 will not compile, because super() invokes the no-arg constructor of the parent class, but the Mammal class doesn't have a no-arg constructor. So compilation fails!


Raghavendra Desoju wrote:The first example on page 242 is getting compiled as well:

And also with this code snippet, the study guide is correct! This code snippet does not compile for a similar reason as the previous code snippet. Because no explicit call to this() (another constructor of the Elephant class) nor super() (a constructor of the parent class Mammal) is added to the no-arg constructor of the Elephant class, the compiler will (try to) add a call to super(). So the compiler tries to add this code to the no-arg constructor of the Elephant classAnd line1 will not compile, because super() invokes the no-arg constructor of the parent class, but the Mammal class doesn't have a no-arg constructor. So compilation fails!

Please note that the Mammal class will successfully compile (in both code snippets), but the compilation of the Elephant class will fail (in both code snippets as well).

Hope it helps!
Kind regards,
Roel
 
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
Raghavendra Desoju wrote:Please disregard this issue.

Too late! I was already typing a lengthy reply
 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic