Tuesday, October 4, 2011

Override Annotation JDK 5 v/s JDK 6

Sometime problems are way beyond your understanding and you could not find the solution for them.

Problem 1

I faced one such problem while I was working on eclipse and JDK5. I created a derived class extending the abstract class. Following is the definition of my abstract class.


public abstract class test {
public abstract void unimplementedMethod(Object[] params);
}

I overrode my abstract class in the class mentioned below.

public class ImplementedTest extends Test{
@Override
public void unimplementedMethod(Object[] params) {
// TODO Auto-generated method stub
}
}

Eclipse was showing a red around the method I had implemented and it was displaying error mentioned below. 
The method unimplementedMethod(Object[]) of type ImplementedTest must override a superclass method

As I see I have not done anything incorrect, I took the exact signature required and pasted in the derived class. After couples of hours of brain storming I found out a import definition of Object. 

import org.omg.CORBA.Object;
public class ImplementedTest extends Test{
@Override
public void unimplementedMethod(Object[] params) {
// TODO Auto-generated method stub
}
}

So how did It manage to sneak into my class? Sometimes eclipse assist you to add required import statements. This time also it appended the import statements, but it appended one additional statement which was not required, and to figure out such a small thing( From the code of 2k lines) it took hours of efforts.

Problem 2
In JDK5, @Override annotation was not working for me. I had an interface and one of my class was implementing the interface.

public interface Test {
public void unimplementedMethod(Object[] params);
}

Class implementing the interface is as follows:

public class ImplementedTest extends Test{
@Override
public void unimplementedMethod(Object[] params) {
// TODO Auto-generated method stub
}
}

Eclipse shows the error mentioned below:
The method unimplementedMethod(Object[]) of type ImplementedTest must override a superclass method

If I remove @override from here it works fine.
If I change my JDK to JDK6, it works without showing any error.
If I change Test interface to test abstract class, it works.

public abstract class Test {
 public abstract void unimplementedMethod(Object[] params);
}

Class implementing the interface is as follows:

public class ImplementedTest extends Test{
@Override
public void unimplementedMethod(Object[] params) {
// TODO Auto-generated method stub
}
}

It works fine. This was non-understandable behavior along with sun has not provided any information about such behavior.

But this behavior has been encounter by different individuals and according to them, in JDK5 when you are implementing the interface then JDK5 do not understand it is override and @Override annotation does not work whereas when you are overriding the method from other class then @Override annotation work without any problem. This confusion was removed later in JDK6. In JDK @override works for interface and general classes.

Source of Reference: