Take a look at the following code sample :
public class Test{
public static void main(String[] args){
System.out.println(new Test());
System.out.println(new Test(){
public String toString(){
return "manual override";
}
});
System.out.println(new Test(){
public String gm(){
return "manual gm";
}
}.gm());
} //end of main method
public String gm(){
return "gm";
}
}
There may be some argument that the toString() method is being overridden in anonymous inner class which is an entirely different class.
But the overriding code still resides in the same class. So, will it be justified to conclude that in some situations [as described above] , the overriding of a method in same class is possible?
The answer is No, you cannot override the same method in one class. The anonymous inner class is a different class.