Results 1 to 3 of 3
  1. #1
    wramhoff is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

  3. #3
    wramhoff is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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

  1. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  2. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  3. Replies: 4
    Last Post: 02-19-2011, 04:32 AM
  4. Replies: 3
    Last Post: 08-13-2010, 04:57 AM
  5. Replies: 2
    Last Post: 11-21-2008, 12:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •