1

Why is NetBeans showing me a hint whenever I try to return a string from a method. Such as-

public String toString(){
  return "Circle: radius= "+radius+" color= "+color;
}

The hint says "add @Override Annotation"

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Shamit Ahmed
  • 45
  • 1
  • 8
  • 1
    because you override a method. If that annotiation is missing and parent function would change then you might run into some problem. The annotation warns you, if the corresponding method doesn´t override the method any more. – SomeJavaGuy Jan 08 '16 at 11:41
  • 1
    @KishanVaghela there is no need to, since it is showing the `toString` method, which initially comes from the `Object`. – SomeJavaGuy Jan 08 '16 at 11:42

1 Answers1

3

The @Override annotation tells the compiler that you are overriding that method which is previously defined in a super class.

It helps the compiler verifying if in deed there is such a method defined in the super class or not. If it is not defined, you will get a compiler error telling you that the method is not defined in the super class.

If you are truely overriding an existing method defined in the super class, your code should work with or without the @Override annotation.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67