Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring a class

 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone
I got aproblem when i try to compile the following code from Kathy Sierra study guide for scjp on chapter 2 self test questions

[Added code tags - see UseCodeTags for details]

When i tried compile this code i got this error massage: class House is public should be declare in a file public class House extends building.

My other problem is:
The output or result of this code is: b h hn x according to the book, what i dont understand is that it did not print "bn" on line 4 and how come it print "x" , they say constructors call their superclass constructors, which execute first, and that constructors can be overloaded, am having difficult trying to understand this, can anyone explain to me so i can understand i wil appriciate your assistance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first problem is exactly what the error message says it is. What's the name of the file you've put it in? Public classes have to be in a file matching the class name. So House needs to be in a file House.java.

For the second part, if you follow the chain of constructors, this is what you get:

- House(String) constructor is called with "x " as the argument.
- This has an explicit call to this(), so it calls the House() constructor
- This has no explicit call to any other constructor, so it calls the no-arg constructor of the superclass, which is Building().
- This prints "b"
- We then pass back down the chain. House() prints "h ", and House(String) prints "hn x ".



 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Matthews

I saved the file as House.java and it compile. Am now clear about the results of this code. I real appriciate your assistance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic