I've implemented an interface to a class and use @Override for the implemented methods.But while compiling i get error saying "Method has to override a method from superclass". Can @Override be used just while extending classes or implementing interfaces too?
@Override indicates a method is being overridden. You don't override methods of an interface - you implement them.
@Override is mostly used to spot typos. Lets say you want to create a specific hashCode() method for a class but name it hashcode() instead. If you tag it with @Override, the compiler will complain and you can fix your typo.
Sun could perhaps have chosen for a similar annotation called @Implement, but it's not that necessary - if you mistype a method you're implementing, the compiler will already complain because the interface's method is not implemented.
[Rob']: @Override indicates a method is being overridden. You don't override methods of an interface - you implement them.
In fact, you can do both. The terms are not exclusive - implementing is a specific type of overriding, where the overridden method is abstract and the overriding method is not. The JLS is less clear on this point than it could be, unfortunately. And furthermore, in JDK 6 they've intentionally changed the behavior of @Override from the description given in JLS 18.104.22.168 and the JavaDoc - @Override does apply now to methods which override interface methods. There are a number of related bug reports about this - here is one where they decide they should change the specs to match the compiler, and here is another where they acknowledge that oops, they still haven't fixed the specs. Would be nice if they get around to that sometime. And let's not forget the JLS... [ January 11, 2008: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Nov 19, 2006
Thanks Rob and Jim, my problem got solved by upgrading the java version.And I needed a quick fix too.