• Post Reply Bookmark Topic Watch Topic
  • New Topic

Learning About Final Method  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm trying to learn about final method and I'm following this code example from this website.  However, I'm getting errors in my code.  I follow the code from this website and I tripled checked it and it matches exactly.  I'm not sure what is causing the error, maybe it's a typo on the website end.

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's quite possible that the code you copied has errors in it. Did you copy-and-paste it? If not, why not? If you retype what you see there then it's easy to miss things.

In your case the opening { of the first class, at line 5 doesn't have a matching } before the second class begins. That's probably what the error messages are getting at, although it would have been more helpful if you had told us what the messages say.
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly, that's a very poorly concocted example.  The names it uses are poorly chosen--that long name explains more what the author is trying to do rather than the ideas/behaviors/relationships that the classes/objects represent in terms of an object-oriented design. The example doesn't even show the difference between declaring that flyNow() method as final or not. If you do nothing else but remove the final keyword in the declaration of that method, the code will still compile and run the same, assuming you correct the syntax error that Paul pointed out.
 
Junilu Lacar
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better example of the use of final methods is when you have a Template method. Here you declare the template method as final because you want to enforce a certain overall flow/algorithm. However, implementation details for parts of the overall flow can be overridden in subclasses.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, it might also help if you tell us what the compiler is complaining about. Just doing a quick look at it, and assuming that it is one Java file, I can see a mismatched set of parenthesis.

Henry
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . the opening { of the first class, at line 5 doesn't have a matching } before the second class begins. . . .
It is possible to “correct” that by appending a } to the end of the code, which converts the second class to an inner class and that will cause even more confusion.
OP: Please always tell us where such code comes from, to avoid copyright problems. Also, as people have already said, you will get faster replies if you supply the full details of your error message. I suspect it said, “class interface or enum expected,” and gave a line at the very end of the file.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!