Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Pool Puzzle Chapter 8 Interfaces and Abstracts  RSS feed

 
Rizal Tabley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The Pool Puzzle in Chapter 8 doesn't seem right.

Below is the code:

The result is
7 class Acts
7 class Clowns
7 class Of76

And not the expected result
5 class Acts
7 class Clowns
7 class Of76

It seems that the code is not reading the method for Acts class.

Is that a problem with Java or the code? I can't figure out what the issue is with the current code.


 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch!

Please be sure to use code tags when posting code.

You'll notice that the public int iMethods() method in class Acts does not override the public int iMethod() method inherited from class Picasso.

In the main() method, the code that runs when you call i[x].iMethod() is the public int iMethod() method inherited from class Picasso.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rizal Tabley wrote:It seems that the code is not reading the method for Acts class.
Is that a problem with Java or the code? I can't figure out what the issue is with the current code.

Tip: Whenever you write a method that is overriding another, ALWAYS use the '@Override' notation, viz:Try it, and see what happens, because THAT should answer your questions.

Winston
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:Please be sure to use code tags when posting code.

I added code tags for you this time. Also please indent your code, not just for our sake, but for yours. You will more easily catch bugs that way.
 
Rizal Tabley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the override notation and the compiler said that the method does not override or implement a method from a super type.

So -
The code telling Java to override the method from an abstract class Picasso which is the super class of the sub class Acts. Yet this is still throwing an error...

I don't get it.

PS - thanks for the code writing tips.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't bother with the @Override annotation for now until you've learnt how to use it.

The problem is that the code you've posted here is not the same code in Head First Java, 2nd Edition, page 234. The code you've posted has a iMethods() method in class Act but the code in the book has a iMethod() method in class Act.

When you've managed to get the code to run, you may want to read page 168 which discusses method overriding.

When you've grasped the concept of method overriding, you may want to learn how to use the @Override annotation to prevent errors when overriding methods.
 
Rizal Tabley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would disagree; I would suggest you should always use the @Override annotation whenever you think you are overriding a method; you simply need to understand how it works. The tutorial link Joe Bishara posted should be good. If you make a tiny spelling error when overriding a method, you will get a complaint from the compiler. Try this, in any class:-
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would suggest you should always use the @Override annotation whenever you think you are overriding a method; you simply need to understand how it works.

I agree.

It is advisable to use the @Override annotation whenever you override a method. However, many books that teach students method overriding don’t start by teaching the @Override annotation. They teach one step at a time. They typically start by teaching inheritance, then method overriding before they introduce the @Override annotation.

The following chapters teach students method overriding but don’t start by teaching the @Override annotation:
  • Head First Java, 2nd Edition (CHAPTER 7: INHERITANCE AND POLYMORPHISM)
  • Oracle Certified Associate Java SE 8 Programmer I Study Guide (CHAPTER 5: CLASS DESIGN)

  • In the book Java in a Nutshell, 6th Edition:

    CHAPTER 3: OBJECT-ORIENTED PROGRAMMING IN JAVA introduces students to method overriding with this code

    CHAPTER 4: THE JAVA TYPE SYSTEM introduces students to the @Override annotation


     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!