• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

What does this paragraph from "Java for Programmers" mean? (annotations)

 
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deitel's book is good all round but some paragraphs are a bit cryptic. For example,

"Annotations have several purposes. For example, when you attempt to override a superclass method, common errors include naming the subclass method incorrectly, or using the wrong number or types of parameters in the parameter list. Each of these problems creates an unintentional overload of the superclass method. If you then attempt to call the method on a subclass object, the superclass’s version is invoked and the subclass version is ignored—potentially leading to subtle logic errors."

Particularly the piece in bold - I'm wondering what he means here. If I have overloaded the base classes method in the subclass and I call it - how is the superclasses version invoked instead? It doesn't make sense to me.
 
Marshal
Posts: 76462
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Try this class:Now execute that main method. You would like it to print Foo, but it doesn’t. People used to have problems that they thought they were overriding methods like that, and making tiny spelling errors. Now add the @Override annotation to that tostring() method, and see what happens. @Override is there to allow the compiler to catch that sort of error.
It is a little bit misleading to say the subclass version is ignored, because to the computer there isn’t a subclass version at all. There is a new and independent method.

What if you had this method instead?That isn’t overridden, but overloaded. You can’t get it to print FooCampbellRitchie or anything like that. Try it, without the @Override annotation, in that Foo class. What happens is that you have overloaded the method when you thought you were overriding it. Again the @Override annotation would have brought that to your attention.
There is more about annotations in the Java Tutorials.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following example:

If you run it, you'll get the output:

What you probably wanted to accomplish is to override doSomething method from superclass in your subclass, and you expect to get the output:

But signatures of methods (in this example method name is different) do not match, so what you actually did is you defined different method in your Sub class that is not invoked in this case, and the original method of Super class remains with the behavior defined in Super.

Now, since the story is in the context of annotations, if you try to add @Override before your method in subclass, you'll get an error so you will know you did it wrong.

Edit: Well, that wasn't fast enough!
 
Iarla O'Riada
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell for explaining that so well. Thanks Kemal for illustrating the code behind the paragraph so closely, that helps me understand it better. I'm glad I understand it now!
 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please to be able to help

Please ask what a race condition is for your next question
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic