Results 1 to 3 of 3
Thread: Overriding the abstract method
- 10-07-2011, 09:30 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Overriding the abstract method
I've seen partial answers to my real question but I still need some advice.
I thought if I used a Comparator, I would quickly and easily develop the comparing methods necessary to sort my private class. Instead, NetBeans shows me the "not abstract and doesn't override method ..." error. Looking around the web for an answer shows up using the @Override annotation and declaring the method in my class. Perhaps I'm over-thinking it, but I need a lesson in what to do.
The abstract method I must override is "int compare(Object, Object);"
It seems to me, I'm expected to write a general compare function for every possible object and data. That's certainly not a doable thing for me; I have enough experience to recognize what I can and cannot do.
I don't want to trick anything into using an empty method and relying on the result but neither do I want to simply through an Exception because I can't implement the body as every object might want. What can I do? Really, what should I do?
I'm also having a little trouble understanding where and how to add the @Override annotation so I'd appreciate help there, too.
Thanks in advance or apologies as necessary.
-
Re: Overriding the abstract method
You use the @Override annotation any time you think you're overriding a super class or interface's method and want the compiler to check to make sure that you are in fact doing this. It is not necessary for the function of your code, but it improves the safety of your code, which is important.
As to just what you need to compare in your Comparator -- it depends. What do you want to do with it? What will you use it for?
Also, if your Comparator is generic, then the compare method signature will be different as the method parameters won't be Object but will be the generic type used when you declare your Comparator class.
- 10-08-2011, 02:48 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Overriding the abstract method
Thank you Fubarable. You've cleared up the @Override syntax.
While awaiting a response here, I was led to Java Generics by my research. Certainly I had been confused but I found a number of tutorials, the best of which informed me that trying to genericize arrays was a no-no. I haven't returned to my code just yet but at least I won't continue stumbling in that blind alley.
As for my Comparator<T>'s compare(T o, T a) method, I will still have to understand how to override the abstract public compare(Object, Object) but perhaps I can postpone that while I get my other methods to work. One annoying part is that I wanted to make this Comparator a "nested" class and make the method protected; of course, the system insists on "public." Oh, well.
I will probably return for more advice, but thanks for your help so far. I'll let ya know.
Similar Threads
-
abstract method-abstract class
By durgaprasad1407 in forum New To JavaReplies: 1Last Post: 04-29-2011, 06:58 PM -
Class is not abstract and does not override abstract method run(com.
By rgeurts in forum New To JavaReplies: 4Last Post: 04-14-2011, 11:42 AM -
Not abstract and doesn't override abstract method keyReleased
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 02-19-2011, 04:32 AM -
SkeletonMidlet is not abstract and does not override abstract method
By just_in_deed in forum CLDC and MIDPReplies: 3Last Post: 08-13-2010, 04:57 AM -
GameImpl is not abstract and does not override abstract method
By Macca07 in forum New To JavaReplies: 2Last Post: 11-21-2008, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks