5 Replies Latest reply on Mar 25, 2008 9:14 PM by 3004

    Inheritance - constructor problem

    807601
      Hello,

      I have a super class Person and a subclass called Player, also a SortPeople driver class. The problem is when I declare super in the Player class, the error comes up with can't find constructor Person. I thought declaring super first would call the Person constructor first, can anyone tell me where I'm going wrong with this?

      Thanks
      public class 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
          
          // five-argument constructor - public Person(String first, String surname, int age, double height, double weight)
          //public Person(String surname)
          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 String toString()
          {
              return ( "First name:" + " " +firstName + " " +
                             "Surname: " + surName + "\n" +
                             "Age: " + ageOfPerson + 
                              "Height: " + heightOfPerson +
                              "Weight of person: " + weightOfPerson );
              
          } // end method toString
          
      
      } 
      public class Player extends Person
      {
          private int noOfGamesPlayed;
          private int noOfGoalsScored;
          
          //public Player(String first, String surname, int age, double height, double weight)
          public Player(int numGamesPlayed, int numGoalsScored)
          {
              super();
              noOfGamesPlayed = numGamesPlayed;
              noOfGoalsPlayed = numGoalsScored;
              
          } // end constructor Player
          
          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
       import java.util.*;
      
      public class SortPeople
      {
          LinkedList<Person> pers = new LinkedList<Person>();
          
          class NameCompare implements Comparator <Person>
          {
              public int compare(Person one, Person two)
              {
                  return one.surName.compareTo(two.surName);
                  
              }
              
          }
          
          public static void main(String [] args)
          {
              new SortPeople().addToList();
              
          }
          
          public void addToList()
          {
              pers.add(new Person("Paul","Hudson",44,14.5,5.15,20,10));
              pers.add(new Person("Neil","Aspinal",68,15.3,6.10));
              pers.add(new Person("John","Mace",33,14.00,6.00));
              pers.add(new Person("John","Lennon",40,15.12,5.14));
              
              System.out.println("as entered:\n" + pers);
              NameCompare nc = new NameCompare();
              
              Collections.sort(pers, nc);
              System.out.println("by name:\n" + pers);
              
              
          }
          
          
          
          
      
              
      }
        • 1. Re: Inheritance - constructor problem
          807601
          super() is called implicitly if you don't add it yourself. If you do add it yourself, you need to call it like you'd call the constructor to the superclass. In this case, Person is the superclass, and it's constructor is:
          public Person(String first, String surname, int age, double height, double weight)
          So your call to super should look just like that, and include all 5 parameters.
          • 2. Re: Inheritance - constructor problem
            3004
            Constructor rules:

            1) Every class has at least one ctor.

            1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

            1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a
            public MyClass() {...} 
            if you want one.

            1.3) Constructors are not inherited.

            2) The first statement in the body of any ctor is either a call to a superclass ctor
            super(...) 
            or a call to another ctor of this class
            this(...) 
            2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor
            super() 
            as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

            2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.
            • 3. Re: Inheritance - constructor problem
              807601
              Ok thanks for the help both of you, did it like this,
              public Player(String first, String surname, int age, double height, double weight,int numGamesPlayed,int numGoalsScored)
                  {
                      super(first, surname, age, height,weight);
                      noOfGamesPlayed = numGamesPlayed;
                      noOfGoalsScored = numGoalsScored;
                      
                  } // end constructor Player
                  
              But my SortPeople class, saying it cannot find the constructor Person, it stops at the first line in the addToList method, why is this the parameter list is two ints???
              {import java.util.*;
              
              public class SortPeople
              {
                  LinkedList<Person> pers = new LinkedList<Person>();
                  
                  class NameCompare implements Comparator <Person>
                  {
                      public int compare(Person one, Person two)
                      {
                          return one.surName.compareTo(two.surName);
                          
                      }
                      
                  }
                  
                  public static void main(String [] args)
                  {
                      new SortPeople().addToList();
                      
                  }
                  
                  public void addToList()
                  {
                      pers.add(new Person("Paul","Hudson",44,14.5,5.15,20,10));
                      pers.add(new Person("Neil","Aspinal",68,15.3,6.10));
                      pers.add(new Person("John","Mace",33,14.00,6.00));
                      pers.add(new Person("John","Lennon",40,15.12,5.14));
                      
                      System.out.println("as entered:\n" + pers);
                      NameCompare nc = new NameCompare();
                      
                      Collections.sort(pers, nc);
                      System.out.println("by name:\n" + pers);
                      
                      
                  }
                  
                  
                  
                  
              
                      
              }
              code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
              • 4. Re: Inheritance - constructor problem
                3004
                Please fix your code tags and indentation.
                • 5. Re: Inheritance - constructor problem
                  3004
                  What constructors does Person have?


                  Why are you trying to create a Person
                  new Person("Paul","Hudson",44,14.5,5.15,20,10))
                  rather than a Player?