This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread 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.
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 ".
Joined: May 19, 2013
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.