This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Sam Peterson

Ranch Hand
+ Follow
since Sep 26, 2019
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sam Peterson

On pages 277-278 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, it talks about how to default methods interact with multiple inheritance. Here is a similar example of code for those pages:





My question is what made the book think that line 15 in Shark2 would work?

I get the error, "Shark2.getNumberOfFins cannot be resolved to a type".
4 months ago
On pages 244-245 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, we have (along with a few additions of my own to the Shark.java file):




On page 244 it says, "You may also use the this keyword to access membes of the parent class that are accessible from the child class, since a child class inherits all of its parent members".

However, recall that on page 242 it also said, "Subclasses may define no-argument constructors even if their parent classes do not, provided the constructor of the child maps to a parent constructor via an explicit call of the super() command".

I've made an assumption that 'constructor of the child maps to a parent constructor' means that the parameter signatures are the same. To test that concept, I added a no-argument constructor on lines 7-9 in the Shark child class (with the super() command on line 8 with an age parameter). According to my concept, that command should have successfully called the parent class constructor (lines 8-10 in the Fish class).

Instead, line 8 in the Shark class gives the error, "The field Fish.age is not visible". Also, despite that error being there, I don't have any errors with the shark object instantiation on line 24. Why?
5 months ago
On page 243 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, we have:





It also reminds us that the compiler first inserts the super() command as the first statement of both the Primate and Ape constructors. That would make sense to me if we didn't have user-defined constructors, but we do. According to the Understanding Compiler Enhancements section on page 241, this:



is the same as this:



if the user doesn't provide any no-argument constructors. However, the Primate, Ape, and Chimpanzee classes all have user-defined constructors, so how is the super() command added automatically?

Also, why is the output:
Ape

Instead of:
Primate
Ape

5 months ago

Jj Roberts wrote:You are getting the error because the constructors are commented out. If something is commented out, it is like it does not exist.


Whatever happened to int age being initialized to a default value of 0? Why doesn't the compiler just assign it to 0?

Campbell Ritchie wrote:...and remember that cert exam books are about the rules of the language and may not always describe best practice.


Oh I know. I'm very painfully aware of that.
5 months ago
Okay folks, lets get back to the topic shall we?

Campbell Ritchie wrote:Please explain what you don&apost understand about my explanation and I shall try again. What happened when you ran the code?



1. The only block that is actually static in Order is line 9. Lines 5 and 6 print that out twice in OrderDriver.

2. In OrderDriver, and instance of order is created not once, but twice on lines 7 and 8. The true mystery regarding that is why s only printed once.

3. If an instance does not include static variables, then the s shouldn't be there, and the print statement on line 9 should only add xyxy to the final output.

4. If the instance created on lines 7 and 8 in OrderDriver DO include the initialization of static variables, then xyxy should actually be sxysxy.

5. And yet, only one of the xy has an s appended to the left of it.

The final output is:
s s sxyxy

So where is the other s, or why is the lone s there at all?



5 months ago
On pages 238 - 242 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, it explains the legal rules about defining constructors in parent and child classes:

Parent Class:

Child Class:

Also, on page 242, it says, "Child classes can define no-argument constructors even if their parent classes do not, as long as the constructor of the child maps to a parent constructor via an explicit call of the super() command."

So why do I get the error, "The constructor Lion() is undefined" on line 29?

Finally, which of the 3 constructor defining methods do you recommend using the most? I like method 2, because it's less code, but it would probably only be useful for testing purposes because of the hard coded values right?
5 months ago
I have a class with 2 user defined constructors:

The output is:
tacb

We start out with "t" from line 4.

There are no static blocks, variables, or methods to worry about.

There are instance blocks, so when called in order, they now give us "tac".

Next we have two user defined constructors. Because of line 9, the first user defined constructor is no longer a default constructor (neither compiler-defined-default nor user-defined-default). But that doesn't explain why the compiler chooses the first constructor (lines 8 - 10) over the second constructor (lines 11 - 12). We can conclude that the first constructor was chosen due to the output being "tacb", but why?

The hardest part to follow is what happens from lines 16 - 18.

Why does the string "f" parameter specified in the Order2 instance call on line 16 not show up in the output? Does it even do anything at all?

What is happening on line 17? Are we assigning our already created order2 object to a new instance? If so, is that why we don't see the "f" parameter as part of the output on line 18?
5 months ago
Campbell, I apologize, but due to goofy forum error detection, I've been forced to use attached images to quote your post and construct my next question. Maybe the moderators should do something about this.
5 months ago
On page227 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, chapter 4 review question 22 asks what the output of the following code would be:




On line 5 in the OrderDriver class, we refer to the Order class for the first time. At that point, the static blocks are initialized in order (or in this case, the only static block defined on lines 8 - 9 in the Order class).

However, because static initialization is only run once, we see "u" printed out from both lines 5 and 6. For this particular case, I assume that's true because there's only one static block (defined on lines 8 - 9). I confirmed this by adding another static keyword on line 10 (which was not in the book's version of the code, so I commented it out). I found that if that static keyword was not commented out, the output would be:
sy sy syxx
With the code the way it is now, the actual output is:
s s sxyxy

The answer key goes on to explain that on line 7 of the OrderDriver class, we create a new Order object. Because of Java's order of initialization, this generates the first sxy. What I don't understand is why the second new Order(); on line 8 only prints xy. If the s of the first sxy was capable of being generated from the static block, then doesn't that mean new Order(); object should be capable of calling both static and instance material, rather than just instance material? If so, then shouldn't xy be sxy instead?

Oh, and where the heck is our s from the print statement on line 9?

If there were no contradicting answers to any of my questions, I believe the output would be:
s s sxysxys
5 months ago

Paul Clapham wrote:If I recall right from your earlier posts, you've already had problems because of confusion between variables and objects. Watching out for that distinction would be helpful.


Alright. Lets shift gears into static then:


Line 14 did something I didn't expect. It printed out 8 instead of 2, even though rope1 and rope2 are different variable names. How can changes from one object update another static variable?
6 months ago
I've set the rope2 object to null:

And the output is:
swing
swing

How can the rope2 object instance, being set to null, still call the method?
6 months ago
On pages 219 - 220 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that challenges you to determine what will and will not compile based on package access:




All of the errors are in School.java:

Line 7: "The field Classroom.globalKey is not visible". Since when is a field with default access not visible to another package? default != private, right?

Line 8: "The constructor Classroom(int, String) is not visible". Even though the roomNumber field has a private access modifier, and the teacherName field has a protected access modifier, we are still importing everything from the my.school package on line 2, so why is visibility still an issue? Or do import statements only make the classes themselves visible, and not necessarily the variables inside of them?
6 months ago
Consider this:

Line 6 gives the error, "Syntax error, insert ";" to complete Statement", even though the book claims that it's valid syntax. What's wrong?
6 months ago
On pages 210 - 211 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that demonstrates lambda expressions. It basically prints out the names of animals based on a boolean of whether they can swim or hop:



On page 211, the book says, "This code uses a concept called deferred execution. Deferred execution means that code is specified now but will run later. In this case, later is when the print() method calls it."

I don't understand. The lambda print method on lines 16 and 17 seem to be listed in the main method just like any method calls would. So what difference is 'deferred execution' supposed to make here?
6 months ago
On page 203 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that is supposed to give output based on the order of initialization:

To my understanding, the order of initialization is:
1. superclass
2. static variables
3. instance variables
4. the constructor

However, the output from the code above is:
1
I'm the statement before creating a new InitializationOrder object.
Torchie
constructor

Why does the print statement for the static variable on line 11 print before the output statement on line 17? The initialization wasn't invoked until line 18.
In other words, why wasn't the output:
I'm the statement before creating a new InitializationOrder object.
1
Torchie
constructor
6 months ago