#Java Constructor and Inheritance, how to do it?

51 messages · Page 1 of 1 (latest)

severe sinew
#

Hello,
I was trying to create the parent class Person. This is my constructor:
```public Person(String name, String fname, String street, Integer hnumber, String city, Integer zip, String mail, String student, int mnumber ){
this.nachname = name;
this.vorname = fname;
this.straße = street;
this.hausnummer = hnumber;
this.stadt = city;
this.plz = zip;
this.mail = mail;
this.student = student;
this.mnumber =mnumber;

}```

now I have a second class called students and I want that Student inherit from Person. Is this the correct way to do it?

public class Student extends Person{
public Student(String name, String fname, String street, Integer hnumber, String city, Integer zip, String mail, String student, int mnumber )
    {
        super(name, fname, street, hnumber, city, zip, mail, student, mnumber);
    }}```
winter raptorBOT
#

This post has been reserved for your question.

Hey @severe sinew! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fierce otter
#

I guess, though with that number of parameters it could be understandable to leave a lot of them to setters instead, unless your classes are immutable

severe sinew
#

thanks for the anwser but what is a setter?

winter raptorBOT
fierce otter
visual terrace
#

You should read about setters and getters if you are working with classes. They help in extracting values from a variable and change it especially if the data members are private.

errant wagon
#

see the classes and objects article here:

winter raptorBOT
errant wagon
#

@severe sinew ^

severe sinew
winter raptorBOT
visual terrace
severe sinew
midnight warren
severe sinew
midnight warren
#

but i dont now what to write inside the super method in the constructor 😦
so you don't want to do:

public Student (Person x){
  super(x);
}

?

severe sinew
#

but i get a Syntax Error

midnight warren
#

let me guess. Your Person class doesn't have a constructor which just defines a single Person object ?

severe sinew
#

it says requiered type String provided Oerson

midnight warren
#

show me

severe sinew
# midnight warren show me

        this.nachname = nachname;
        this.vorname = vorname;
        this.straße = straße;
        this.hausnummer = hausnummer;
        this.stadt = stadt;
        this.plz = plz;
        this.mail = mail;
        this.student = student;

    }```
midnight warren
#

I meant a Person constructor like this:

public Person(Person person) {
    //Initialise Person object
}
severe sinew
#

In which class?

midnight warren
#

in the Person class

severe sinew
#

but i dont have a person to pass, because I ve to pass it a name, fname and other Attributes.

midnight warren
#

so you don't have an instance of Person that you can pass to the Student constructor ?

severe sinew
#

in my main class i created an instance of Person with the constructor above, but i cant pass the instance.

#

to the constructor in the student class

midnight warren
#

yeah then create the two constructors I just showed you. One constructor for Student which accepts an instance of Person and in the Person class you create a constructor which just accepts another instance of Person

severe sinew
#

Okay thanks but will I get then 2 instances of Person in my main class?

winter raptorBOT
midnight warren
#

you cann assign that Student object to a Person variable again but it will still be of type Student

severe sinew
#

And this will stay empty?
public Person( Person person){}

#

But i created the first instance of person with the first constructor, then i pass this instance to create a new instance of Person, right? And that i will pass to Student. Did i understand it correct?

midnight warren
#

this won't stay empty. You will have to create another Person object. If that's what you meant then yes, you will create a new instance of Person

severe sinew
#

Okay but what do i ve to write in the body?

midnight warren
#

whatever you need to do to get the Person instance to a default state. Popluate class fields, maybe some logic. Or just call another constructor with its respective args.

severe sinew
#

?

midnight warren
#

yeah or you do something like:
this(person.name,person.fname,...) and so on.

severe sinew
#

Ahh this is mindblowing for me rn. Okay but how do i handle private Attributes

#

With getMethods?

errant wagon
#

with getters yeah

severe sinew
#

Im really thankful to you all, I will try to implement it now.

winter raptorBOT