File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes @override annotation - >> Abstract Class and Interface Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "@override annotation - >> Abstract Class and Interface Implementation" Watch "@override annotation - >> Abstract Class and Interface Implementation" New topic
Author

@override annotation - >> Abstract Class and Interface Implementation

Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is not a SCJP topic. So I'm moving to Java in General (Intermediate)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It really doesn't seem correct does it? So it must be a bug.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
Thanks Barry, i should have checked it in the bug list before posting.. Sorry for that..

Regards,
Tripter
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @override annotation - >> Abstract Class and Interface Implementation
 
Similar Threads
what is interface ?
How many objects are created ?
Newbie question
abstract and interfaces
how do abstract classes like NumberFormat call the methods from their subclasses?