aspose file tools*
The moose likes Beginning Java and the fly likes Using @ Override while implementing an interface. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using @ Override while implementing an interface." Watch "Using @ Override while implementing an interface." New topic
Author

Using @ Override while implementing an interface.

Rio Reva
Greenhorn

Joined: Nov 19, 2006
Posts: 14
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?

I am using java version "1.6.0-beta".

Could somebody please help me?
Java Lover
Greenhorn

Joined: Nov 23, 2006
Posts: 12
Yes,u must override the methods in interface, otherwise make the extending class as abstract. or simply override the methods.No other go.
Rio Reva
Greenhorn

Joined: Nov 19, 2006
Posts: 14
Thanks, my issue got fixed after I upgraded to java version "1.6.0_03".

I am not sure, but I think in the beta version, @Override annotation was not compiling while used with implementing an interface.It worked fine when I tried to extend one class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

@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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 9.6.1.4 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
Rio Reva
Greenhorn

Joined: Nov 19, 2006
Posts: 14
Thanks Rob and Jim, my problem got solved by upgrading the java version.And I needed a quick fix too.

thanks to all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using @ Override while implementing an interface.