File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes declaring a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "declaring a class" Watch "declaring a class" New topic

declaring a class

Harold Ndou
Ranch Hand

Joined: May 19, 2013
Posts: 34
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

Joined: Apr 06, 2010
Posts: 4544

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

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

Joined: May 19, 2013
Posts: 34
Thanks so much Matthews

I saved the file as and it compile. Am now clear about the results of this code. I real appriciate your assistance.
I agree. Here's the link:
subject: declaring a class
It's not a secret anymore!