how do I pass subclass parameters into the superclass private variables?

15,230

First, you need to define a constructor for Person:

public Person(String startName, int startDollars, boolean startMood)
{
    name = startName;
    dollars = startDollars;
    mood = startMood;
}

Then you can pass data up from the Student constructor using super(...):

public Student(String startName, int startDollars, boolean startMood, String major)
{
    super(startName, startDollars, startMood);
    . . .
}

Alternatively, you can define setters in the Person class and invoke them from the Student constructor.

public class Person
{
     private String name; //holds the name of the person
     private boolean mood; //holds the mood happy or sad for the person
     private int dollars; //holds their bank account balance

     public void setName(String name) {
         this.name = name;
     }
     // etc.
}
Share:
15,230
reeltempting
Author by

reeltempting

I have taken a couple classes in java and a development class in Android. I am about a year into coding and beyond novice but far from expert.

Updated on June 04, 2022

Comments

  • reeltempting
    reeltempting almost 2 years

    I am confused on how to get parameters from new object instances to also flow into the super class to update the private fields in teh super class.

    So I am in an advanced Java class and I have homework that requires a "Person" Super Class and a "Student" subclass that extends Person.

    The Person class stores the student name BUT it is the Student class constructor that accepts the Person name.

    assume no method in Person to make a variable method update...like subClassVar = setSuperClassVar();

    EX:

    public class Person
    {
         private String name; //holds the name of the person
         private boolean mood; //holds the mood happy or sad for the person
         private int dollars; //holds their bank account balance
    }
    
    class Student extends Person //I also have a tutor class that will extend Person as well
    {
         private String degreeMajor //holds the var for the student's major they have for their degree
    
         Public Student(String startName, int startDollars, boolean startMood, String major)
         {
              degreeMajor = major;  // easily passed to the Student class
              name = startName; //can't pass cause private in super class?
              mood = startMood; //can't pass cause private in super class?
              dollars = startDollars; // see above comments
              // or I can try to pass vars as below as alternate solution...
              setName() = startName; // setName() would be a setter method in the superclass to...
                                     // ...update the name var in the Person Superclass. Possible?
              setMood() = startMood; // as above
              // These methods do not yet exist and I am only semi confident on their "exact"...
              // ...coding to make them work but I think I could manage.
         }   
    }
    

    The instructions for the homework were a bit vague in terms of how much changing to the superclass of Person I am allowed to make so if you all believe a good solid industry accepted solution involves changing the superclass I will do that.

    Some possible examples I see would be to make the private vars in Person class "protected" or to add setMethods() in the person class and then call them in the sub class.

    I am also open to general concept education on how to pass subclass contstructor parameters to a super class...and if possible do that right in the constructor portion of the code.

    Lastly, I did search around but most of the similiar questions were really specific and complicated code....I couldnt find anything straight forward like my example above...also for some reason the forum post did not clump all of my code together so sorry for the confusing read above.

    Thanks all.

  • reeltempting
    reeltempting over 11 years
    Dude...elementary dear watson...TYVM, just so refreshing to ask a direct questions and get a direct answer. I shall continue coding.