This discussion is archived
3 Replies Latest reply: May 2, 2009 12:41 AM by 807588 RSS

Problems with Abstract classes and Interfaces

807588 Newbie
Currently Being Moderated
Okay, so here's the gist of my issue.

public abstract class A implements Comparable<A> {...public into compareTo(){...}...}

public class B extends A {... public int compareTo(){...}...}

public class C extends A {... public int compareTo(){...}...}

I get errors when I attempt to compile. Netbeans and whatever compiler is running on the university computers both say that Class B and C are not abstract and that they do not override compareTo() from java.lang.comparable.

wtf? why? I don't get it. :(

Can post all of my code if it will help.
  • 1. Re: Problems with Abstract classes and Interfaces
    807588 Newbie
    Currently Being Moderated
    Can post all of my code if it will help.
    Not all of the code! But enough to illustrate the problem. For instance having both B and C is probably unnecessary. So, for instance, a minimal example might be:
    public abstract class A implements Comparable<A> {
        public int compareTo() {
            return 0;
        }
    }
    
    class B extends A {
        public int compareTo(){
            return 0;
        }
    }
    The compiler message I see for this is:
    The type B must implement the inherited abstract method Comparable<A>.compareTo(A)
    Does this message make sense? It's saying it wants a compareTo(A) method, not a compareTo() or even a mere compareTo(Object). That's because you declared A to implement Comparable<A>.
  • 2. Re: Problems with Abstract classes and Interfaces
    807588 Newbie
    Currently Being Moderated
    Okay, first I'll post my "A" and then my "B".
    public abstract class Player implements Comparable<Player>
    {
        public abstract int compareTo(Player p);
        /*{
            int pGold = 0;
            pGold = p.getGold();
    
            if (this.gold > pGold)
                return 1;
            else if (this.gold < pGold)
                return -1;
            else return 0;
        }*/
    
         public abstract Player fight(Player p);
    }
    Here is my B
    public class Warrior extends Player
    {
        public int compareTo(Player p);
        {
            int ownGold = 0, pGold = 0;
            ownGold = this.getGold();
            pGold = p.getGold();
    
            if (ownGold > pGold)
                return 1;
            else if (ownGold < pGold)
                return -1;
            else return 0;
        }
    
        @Override
        public Player fight(Player p)
        {
            double moddedSkillParameter = this.getSkill() / (this.getSkill() + p.getSkill());
            double testValue = rand.nextDouble()*10;
    
            if (moddedSkillParameter < testValue)
                return p;
            else return this;
        }
    }
    Um... I'm not sure if this affects your ethics about this, but this is the final hangup for my final school project. It's due in 5 hours. :( I've wracked my brains on this.

    Any help you're willing to give would be wonderful.

    [EDIT] lol, sorry. I forgot to post my error message.

    Netbeans tells me: "Warrior is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable"

    [EDIT] lol, I fixed it...

    The code posted reflects all changes that fixed my issue... lolz.

    Edited by: code_reaper on May 2, 2009 12:26 AM
  • 3. Re: Problems with Abstract classes and Interfaces
    807588 Newbie
    Currently Being Moderated
    I'm glad you've got it working.

    I didn't see your post before you edited it, so I don't know how you declared Player. But if you declared it as "implements Comparable<Player>" then the NB compiler message is strange. It's not that you don't have a compareTo(Object) method but rather - as you have found - that you didn't have compareTo(Player).