• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Java Ed. 2 p257 Sharpen Your Pencil  RSS feed

 
Mimi Chang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm new here.

I've been working through HeadFirst Java and am wondering if anyone can tell me if I've gotten the answers right to the exercises on page 257 (since the book doesn't give you the answers).

QUESTION: Some of the constructors in the SonOfBoo class will not compile. See if you can recognize which constructors are not legal.

INCLUDED CODE:



CONSTRUCTORS:





// Nothing wrong here. The superclass has a Boo(int i) constructor. Not using the String s also shouldn't matter...I think.

// Still nothing wrong.... The compiler should automatically insert a super() statement

// Error. There isn't a Boo constructor that takes in two strings.
// The error should read: cannot resolve symbol
symbol : constructor Boo (java.lang.String, java.lang.String)

// This one seems fine

// Error. There is no Boo constructor that takes in an int and string (in that order).
//Error should read: cannot resolve symbol
symbol : constructor Boo (Int, java.lang.String)

}

Any feedback is deeply appreciated. Thanks!!!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mimi Chang wrote:Any feedback is deeply appreciated. Thanks!!!

Does Boo have a no-args constructor?

And where/why do you think that might be important?

Winston
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your code, and doesn't it look better. I missed out the last } by mistake. Also added numbers eg //3
Nos 1 2 3 5 6 7: all correct.

No 4 incorrect. I shall leave you to work out why. Hint: Java Language Specification section.
 
Mimi Chang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops--I'll use code tags next time! Sorry!

Thanks so much for the quick responses! I think I see why #4 is wrong now. The Boo class doesn't have a no-args constructor. But, the compiler automatically inserts the no-args super() statement. So even though the empty constructor in #4 calls super() it causes an error because a no-args constructor doesn't exist in Boo. Ahaa.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!