• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

@override annotation - >> Abstract Class and Interface Implementation  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am not clear on @override annotation. Let me explain with a scenario.



In the above code, when MyFirstClass extends an abstract class and implements a method getNumber(), using @override is fine.

But in MySecondClass which is implementing MyInterface and implementing the method getNumber(), using @override gives a compilation error.

My confusion is even all the methods in the interface are internally abstract also the interface itself is an abstract to the compiler. Why is @override tag used only when implementing methods of abstract classes but not when implementing interface methods.

Please clear this.

Regards,
Tripter
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a SCJP topic. So I'm moving to Java in General (Intermediate)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really doesn't seem correct does it? So it must be a bug.
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this is unnecessarily confusing, actually. @override is useless when you're "overriding" an abstract method. The usefulness of @override is when you're overriding a non- abstract method, but you misspell the method name or change the parameter list, and as a result, you don't really override it. Then you can get a warning or error. Since making the same mistake when overriding an abstract method would result in a compile error anyway, it's really not useful, and they could have (in theory, at least) defined this annotation to only be applicable to overrides of non-abstract methods.
 
Srivatsa Katta
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry, i should have checked it in the bug list before posting.. Sorry for that..

Regards,
Tripter
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
@override is useless when you're "overriding" an abstract method.



No, it's not.

Imagine you introduce a new abstract method, and accidentally a subclass already has a method with the same signature. Would be good to notice, wouldn't it?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I can see that. So that would require a couple things:

1) A policy requiring @Override on every overriding method;

2) A tool that looked at all your class files and enforced this policy. This tool would be the thing to catch this case you mention; javac doesn't have a switch to enforce this, does it?

Does your team do something like this?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Yes, I can see that. So that would require a couple things:

1) A policy requiring @Override on every overriding method;

2) A tool that looked at all your class files and enforced this policy. This tool would be the thing to catch this case you mention; javac doesn't have a switch to enforce this, does it?

Does your team do something like this?



Eclipse does it (optionally - either as a warning or an error). I think we currently have it set to "warning", because of a lot of legacy code.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IntelliJ has the option as well, under Errors -> IDE Profiles -> Inspections -> Class Structure -> Missing @Override annotation.

If one wanted to enforce this as part of build, I imagine there's a way to do it with apt or a CheckStyle extension. Seems like there probably already is such a tool somewhere, but my brief round of googling didn't turn it up.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!