Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using @ Override while implementing an interface.

 
Rio Reva
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Rio Reva
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and Jim, my problem got solved by upgrading the java version.And I needed a quick fix too.

thanks to all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic