• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in method signature

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I just beginning learn object oriented programming and I've developed this class and works well, but the last method flag this suggestion "Add@Override Annotation"
can anyone tell me why???
further, I'd be grateful if you give me any advices about this class
Thanks in advance
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm just learning java too.. as far as I know this is because your method has the signature toString which means you are overriding the toString method of the object class which is the implicit superclass of all other classes. So this is just a suggestion for you to add the annontation to you code to say your are overriding a method of the superclass.

Hope this helps...
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations. This one isn't required, but it's a good practice to always use the @Override annotation. That prevents you from making typos:
Since the method name is hashCode, with capital C, this is not overriding so there will be a compiler error.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A general remark about your code:

The fan speed is set by an integer and you have defined 3 constants for the speed settings. However it's perfectly legal to call it with: fan.setSpeed(5000); (if 3 is fast then how fast would 5000 be ). A better way would be to define an enum.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it might be a good idea to make the final fields public. Then other classes can invoke them to set the fan's speed.

As an atlernative, you can set bounds and refuse to alter the speed if it is outwith the permitted speed range (in your case 0-3 inclusive, using 0 as "stop"). Wouter's suggestion of an nenum is far better, however.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic