Enum Anatomy

Rule 3:Always override the methods from Super-interface and Super-Class

Failing to override methods of super class and super-interface your are preparing to fail the implementors of these methods.
example: adding chars in Set:
class Bigram{
Char first;
Char second;
//consturctor for setting
Bigram(…){
//set things
}
public void testMethod(){
Set s=new HashSet<>();
for(char ch=’a’:ch<=’z’:ch++){
s.add(ch,ch);
}

}
//Not overriden but overloaded
boolean equals(Bigram b ){
return b.first==first&&b.second==second;
}
//Not overriden but overloaded
int hashcode(){
return 31*something;}
}

ShortFall: set calls Object equals method and compares only for object equality which is nothing more than == checking hence returns ‘a’==’a’ to be unequal

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment