CRITICAL
Rule Definition
When programming it is very easy to make a mistake when naming a method to override or in its signature. This may occur when writing the inherited class, but also when changing the signature of the basic class to add a new parameter or when changing the type of a parameter. One of the more typical examples is the overriding of equals methods with an argument type that is different from the Object class.
Remediation
Fix the name of the method or the signature and if you use JSE 5.0 or later add the @Override annotation to inform the compiler that the method is meant to override a method declared in a superclass. If the method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.
Violation Code Sample
class Example {
int firstMethod (String str) {...}
void secondMethod (int value, boolean b) {...}
}
class ExampleExtended extends Example{
int firstmethod (String str) {...} // VIOLATION because of 'm' that is not in uppercase as the parent class method
void secondMethod (int value) {...} // VIOLATION
}
Fixed Code Sample
class Example {
int firstMethod (String str) {...}
void secondMethod (int value, boolean b) {...}
}
class ExampleExtended extends Example{
@Override // only for JSE 5 or later
int firstMethod (String str) {...} // FIXED
@Override // only for JSE 5 or later
void secondMethod (int value, boolean b) {...} // FIXED
}
Reference
[1] http://www.artima.com/lejava/articles/equality.html - it explains the consequences when equals for example is not well overriden
Related Technologies
JEE
Technical Criterion
Programming Practices - OO Inheritance and Polymorphism
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.