invoke a superclass constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bill226
    New Member
    • Aug 2007
    • 2

    #1

    invoke a superclass constructor

    I have to get the subclass to use the superclass constructor, heres my code:

    [CODE=java]public class Student
    {

    // instance variables
    private String name;
    private int pin;
    private boolean newToOU;
    private Map<String, List<Integer>> courses;his id the super class


    public Student(String aName)
    {
    super();
    Student.counter ++;
    this.name = aName;
    this.pin = Student.counter ;
    this.newToOU = true;
    this.courses = new HashMap<String, List<Integer>>( );

    this is the subclass

    public class StudentAdmin extends Student
    {
    /* instance variables */
    private Map<Integer, String> allStudents;
    private Set<Integer> continuingStude nt;
    private Set<Integer> newStudent;
    public StudentAdmin()
    {
    super();
    this.allStudent s = new TreeMap<Integer , String>();
    this.continuing Student = new TreeSet<Integer >();
    this.newStudent = new TreeSet<Integer >();[/CODE]

    when I compile this code I get an error: cannot find symbol - constructor Student()

    Any help would be gratefully accepted
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    There's an article in the Java Articles section about the Liskov Substitution Principle.
    A StudentAdmin is definitely *not* a student. Your class hierarchy is all wrong.

    kind regards,

    Jos

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      [CODE=java]
      public class Student
      {

      // instance variables
      private String name;
      private int pin;
      private boolean newToOU;
      private Map<String, List<Integer>> courses;his id the super class


      public Student(String aName)
      {
      super();
      Student.counter ++;
      this.name = aName;
      this.pin = Student.counter ;
      this.newToOU = true;
      this.courses = new HashMap<String, List<Integer>>( );
      [/CODE]
      [CODE=java]
      public class StudentAdmin extends Student
      {
      /* instance variables */
      private Map<Integer, String> allStudents;
      private Set<Integer> continuingStude nt;
      private Set<Integer> newStudent;
      public StudentAdmin()
      {
      super();
      this.allStudent s = new TreeMap<Integer , String>();
      this.continuing Student = new TreeSet<Integer >();
      this.newStudent = new TreeSet<Integer >();
      }
      [/CODE]
      Originally posted by bill226
      when I compile this code I get an error: cannot find symbol - constructor Student()
      The Class Student doesn't have a costructor named Student(), only one called Student(String aName). So either have another Constructor in the Student class, or in StudentAdmin() call super("some String");.

      Comment

      • praveen2gupta
        New Member
        • May 2007
        • 200

        #4
        Originally posted by bill226
        I have to get the subclass to use the superclass constructor, heres my code:

        public class Student
        {

        // instance variables
        private String name;
        private int pin;
        private boolean newToOU;
        private Map<String, List<Integer>> courses;his id the super class


        public Student(String aName)
        {
        super();
        Student.counter ++;
        this.name = aName;
        this.pin = Student.counter ;
        this.newToOU = true;
        this.courses = new HashMap<String, List<Integer>>( );

        this is the subclass

        public class StudentAdmin extends Student
        {
        /* instance variables */
        private Map<Integer, String> allStudents;
        private Set<Integer> continuingStude nt;
        private Set<Integer> newStudent;
        public StudentAdmin()
        {
        super();
        this.allStudent s = new TreeMap<Integer , String>();
        this.continuing Student = new TreeSet<Integer >();
        this.newStudent = new TreeSet<Integer >();

        when I compile this code I get an error: cannot find symbol - constructor Student()

        Any help would be gratefully accepted
        Hi
        You have not defined the constructor of Student class. For calling the default constructor in the sub classes you should define the default constructor in Student class.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by praveen2gupta
          For calling the default constructor in the sub classes you should define the default constructor in Student class.
          That most certainly isn't true: the default ctor of a superclass is called from *any*
          ctor in the derived class where no explicit super( ... ) is invoked as its first statement.
          Or maybe I completely misunderstood your answer.

          kind regards,

          Jos

          Comment

          • BigO
            New Member
            • Aug 2007
            • 1

            #6
            A "default" constructor is only created when no constuctor is explicitly defined. In this case, a constructor is defined, therefore, there is not a no-arg constructor for Student.
            Praveen is using the terminology "default constructor" when s/he (sorry) means a "no arg" constructor.
            The *no arg* constructor of a superclass is called from *any* ctor in the derived class where no explicit "super" is invoked. This is not necessarily the "default" constructor, it can be a user defined *no arg* constructor.

            "nepomuk" provided the correct solution.
            Last edited by BigO; Aug 29 '07, 05:08 AM. Reason: to point out the solution

            Comment

            • shaileshkumar
              New Member
              • Aug 2007
              • 36

              #7
              one important point to note:

              whether solves your problem or not,

              when you inherit a class

              constructors are not inherited.

              Comment

              Working...