5 Replies Latest reply: Apr 19, 2008 2:58 PM by 807601 RSS

    Inheritance and Comparable interface

    807601
      Hello,

      I have a Person class and a Player class. The Player class inherits from the Person class. I have written the Player class as such:
      import java.util.*;
      
      public class Player extends Person implements Comparable
      {
          private int noOfGamesPlayed;
          private int noOfGoalsScored;
          
          public Player(String first, String surname, int age, double height, double weight, int noOfGamesPlayed, int noOfGoalsScored)
          {
              super(first,surname,age,height,weight);
              this.noOfGamesPlayed = noOfGamesPlayed;
              this.noOfGoalsScored = noOfGoalsScored;
              
          } // end constructor Player
          
          
          public int compareTo(Player ply)
          {
              return noOfGamesPlayed.compareTo(ply.getGamesPlayed());
              
          }
          
          public void setGamesPlayed( int gamesPlayed)
          {
              noOfGamesPlayed = gamesPlayed;
          } // end method setGamesPlayed
          
          public int getGamesPlayed()
          {
              return noOfGamesPlayed;
          } // end method getGamesPlayed
          
          public void setGoalsScored( int goalsScored)
          {
              noOfGoalsScored = goalsScored;
          } // end method setGoalsScored
          
          public int getGoalsScored()
          {
              return noOfGoalsScored;
          } // end method getGoalsScored
          
      } // end class Player
      The error message I get is 'java.lang.Comparable cannot be inherited with different arguments:<> and <Person>'

      I don't understand the error message, can somebody please help me.
        • 1. Re: ???
          807601
          I apologize because you can not give a hand goes I would like to know where can I ask for a connection problem giava on the control panel have completed the manual removal (erasing all folders named java), and now does not erase or on the same both installation applications this forum and really complex
          [?]
          • 2. Re: Inheritance and Comparable interface
            807601
            You may try changing the Comparable statement in the first line to use generics. From this:
            public class Player extends Person implements Comparable
            to this:
            public class Player extends Person implements Comparable<Player>
            Let me know if this helps.
            • 3. Re: ???
              807601
              Hawk85 wrote:
              I apologize because you can not give a hand goes ...
              ps: ignore this troll.
              • 4. Re: Inheritance and Comparable interface
                807601
                Hello Encephalopathic,

                No it still gives the same error message, with a slight change 'java.lang.Comparable cannot be inherited with different arguments:<Player> and <Person>.

                The spec says 'implement the appropriate Comparable interface' . Also I forgot to mention was the Person class has the following code:
                public class Person implements Comparable<Person>
                {
                    protected String firstName;             // First Name
                    protected String surName;               // surname of person
                    protected int ageOfPerson;               // age of person
                    protected double heightOfPerson;        // height of person
                    protected double weightOfPerson;        // weight of person
                    
                    public int compareTo(Person p)
                    {
                        return surName.compareTo(p.getSurName());
                        
                    }
                    
                    
                    // five-argument constructor
                    public Person(String first, String surname, int age, double height, double weight)
                    {
                        firstName = first;
                        surName = surname;
                        ageOfPerson = age;
                        heightOfPerson = height;
                        weightOfPerson = weight;
                        
                    } // end of five-argument constructor
                    /////////////////////////////////////////////////////////////
                    public void setFirstName( String first)
                    {
                        firstName = first;
                    } // end method setFirstName
                    
                    public String getFirstName()
                    {
                        return firstName;
                    } // end method getFirstName
                    /////////////////////////////////////////////////////////////////////
                    public void setSurName( String surname )
                    {
                        surName = surname;
                    } // end method setSurName
                    
                    public String getSurName()
                    {
                        return surName;
                    } // end method getSurName
                    //////////////////////////////////////////////////////////////////////////////
                    public void setAge( int age )
                    {
                        ageOfPerson = age;
                    } // end method setAge
                    
                    public int getAge()
                    {
                        return ageOfPerson;
                    } // end method getAge
                   //////////////////////////////////////////////////////////////////////////////////// 
                    public void setHeight( double height )
                    {
                        heightOfPerson = height;
                    } // end method setHeight
                    
                    public double getHeight()
                    {
                        return heightOfPerson;
                    } // end method getHeight
                    ///////////////////////////////////////////////////////////////////////////////////////
                    public void setWeight( double weight)
                    {
                        weightOfPerson = weight;
                    } // end method setWeight
                    
                    public double getWeight()
                    {
                        return weightOfPerson;
                    } // end method getWeight
                    
                    
                    
                    
                    
                    public String toString()
                    {
                        return ( "First name: " + firstName +
                                       "Surname: " + surName +
                                       "Age: " + ageOfPerson + 
                                        "Height: " + heightOfPerson +
                                        "Weight of person: " + weightOfPerson );
                        
                    } // end method toString
                    
                } // end class Person
                 
                This class does not give any errors
                • 5. Re: Inheritance and Comparable interface
                  807601
                  Then I think that you're stuck using Comparable<Person>.