trying to call superclass method in subclass












1














Probably a pretty noob question, but I cant figure it out. I have a class Person to store a name that is input from the keyboard



public class Person {
private String firstName;
private String lastName;

public Person()
{
firstName = "";
lastName = "";
}

public Person(String first, String last)
{
setName(first, last);
}

public String toString()
{
return(firstName + " " + lastName);
}

public void setName(String first, String last)
{
firstName = first;
lastName = last;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}
}


I am trying to call the toString method in a subclass called Patient



public class Patient extends Person {
private int patientID, patientAge;
public Patient()
{
patientID = 0; //for a different part of the class that works
patientAge = 0;
}
@Override
public String toString()
{
return ("Patient Name: "+super.toString());
}
}


I cannot get it to output the name in main when I call the toString method from the Patient class, but when I tested it, it output the name when I call the toString method from the Person class.
The method call in main looks like Patient pnt = new Patient(); System.out.print(Pnt.toString());
it prints out in the console "Patient Name: ". Any feedback on what I am doing wrong or ideas on how to get it to work










share|improve this question




















  • 3




    How are you constructing your Patient object? Looks like the first and last name aren't getting set.
    – Mike
    Nov 19 '18 at 19:39








  • 1




    Please post your main method
    – mettleap
    Nov 19 '18 at 19:40










  • Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
    – GhostCat
    Nov 19 '18 at 19:56










  • the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
    – stelios.anastasakis
    Nov 21 '18 at 8:13


















1














Probably a pretty noob question, but I cant figure it out. I have a class Person to store a name that is input from the keyboard



public class Person {
private String firstName;
private String lastName;

public Person()
{
firstName = "";
lastName = "";
}

public Person(String first, String last)
{
setName(first, last);
}

public String toString()
{
return(firstName + " " + lastName);
}

public void setName(String first, String last)
{
firstName = first;
lastName = last;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}
}


I am trying to call the toString method in a subclass called Patient



public class Patient extends Person {
private int patientID, patientAge;
public Patient()
{
patientID = 0; //for a different part of the class that works
patientAge = 0;
}
@Override
public String toString()
{
return ("Patient Name: "+super.toString());
}
}


I cannot get it to output the name in main when I call the toString method from the Patient class, but when I tested it, it output the name when I call the toString method from the Person class.
The method call in main looks like Patient pnt = new Patient(); System.out.print(Pnt.toString());
it prints out in the console "Patient Name: ". Any feedback on what I am doing wrong or ideas on how to get it to work










share|improve this question




















  • 3




    How are you constructing your Patient object? Looks like the first and last name aren't getting set.
    – Mike
    Nov 19 '18 at 19:39








  • 1




    Please post your main method
    – mettleap
    Nov 19 '18 at 19:40










  • Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
    – GhostCat
    Nov 19 '18 at 19:56










  • the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
    – stelios.anastasakis
    Nov 21 '18 at 8:13
















1












1








1


1





Probably a pretty noob question, but I cant figure it out. I have a class Person to store a name that is input from the keyboard



public class Person {
private String firstName;
private String lastName;

public Person()
{
firstName = "";
lastName = "";
}

public Person(String first, String last)
{
setName(first, last);
}

public String toString()
{
return(firstName + " " + lastName);
}

public void setName(String first, String last)
{
firstName = first;
lastName = last;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}
}


I am trying to call the toString method in a subclass called Patient



public class Patient extends Person {
private int patientID, patientAge;
public Patient()
{
patientID = 0; //for a different part of the class that works
patientAge = 0;
}
@Override
public String toString()
{
return ("Patient Name: "+super.toString());
}
}


I cannot get it to output the name in main when I call the toString method from the Patient class, but when I tested it, it output the name when I call the toString method from the Person class.
The method call in main looks like Patient pnt = new Patient(); System.out.print(Pnt.toString());
it prints out in the console "Patient Name: ". Any feedback on what I am doing wrong or ideas on how to get it to work










share|improve this question















Probably a pretty noob question, but I cant figure it out. I have a class Person to store a name that is input from the keyboard



public class Person {
private String firstName;
private String lastName;

public Person()
{
firstName = "";
lastName = "";
}

public Person(String first, String last)
{
setName(first, last);
}

public String toString()
{
return(firstName + " " + lastName);
}

public void setName(String first, String last)
{
firstName = first;
lastName = last;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}
}


I am trying to call the toString method in a subclass called Patient



public class Patient extends Person {
private int patientID, patientAge;
public Patient()
{
patientID = 0; //for a different part of the class that works
patientAge = 0;
}
@Override
public String toString()
{
return ("Patient Name: "+super.toString());
}
}


I cannot get it to output the name in main when I call the toString method from the Patient class, but when I tested it, it output the name when I call the toString method from the Person class.
The method call in main looks like Patient pnt = new Patient(); System.out.print(Pnt.toString());
it prints out in the console "Patient Name: ". Any feedback on what I am doing wrong or ideas on how to get it to work







java subclass superclass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 20:26







user9643025

















asked Nov 19 '18 at 19:37









user9643025user9643025

63




63








  • 3




    How are you constructing your Patient object? Looks like the first and last name aren't getting set.
    – Mike
    Nov 19 '18 at 19:39








  • 1




    Please post your main method
    – mettleap
    Nov 19 '18 at 19:40










  • Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
    – GhostCat
    Nov 19 '18 at 19:56










  • the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
    – stelios.anastasakis
    Nov 21 '18 at 8:13
















  • 3




    How are you constructing your Patient object? Looks like the first and last name aren't getting set.
    – Mike
    Nov 19 '18 at 19:39








  • 1




    Please post your main method
    – mettleap
    Nov 19 '18 at 19:40










  • Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
    – GhostCat
    Nov 19 '18 at 19:56










  • the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
    – stelios.anastasakis
    Nov 21 '18 at 8:13










3




3




How are you constructing your Patient object? Looks like the first and last name aren't getting set.
– Mike
Nov 19 '18 at 19:39






How are you constructing your Patient object? Looks like the first and last name aren't getting set.
– Mike
Nov 19 '18 at 19:39






1




1




Please post your main method
– mettleap
Nov 19 '18 at 19:40




Please post your main method
– mettleap
Nov 19 '18 at 19:40












Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
– GhostCat
Nov 19 '18 at 19:56




Just for the record, as newbies often forget about that: please dont forget to accept an answer at some point!
– GhostCat
Nov 19 '18 at 19:56












the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
– stelios.anastasakis
Nov 21 '18 at 8:13






the problem is that u dont set the name. now the solutions are already suggested on comments. either use your setters or telescoping constructors and set the name in your main or subclasses constructor(s). also remember that the setters are inherited so u can construct a patient, use setters for name and print it
– stelios.anastasakis
Nov 21 '18 at 8:13














5 Answers
5






active

oldest

votes


















1














Here:



public Person()
{
firstName = "";
lastName = "";
}


Your subclass is missing a reasonable call to a super class constructor. So when you instantiate your Patient objects, the above constructor is used, and all patients end up with "" as first and last name!



When you create a Patient, then a patient should have a name, too! But your constructor in Patient only sets the Patient related fields. And implicitly, the default super constructor is called. Therefore the Person fields are all set to be empty strings!



A much better approach would look like this:



class Person {
private final String firstName;
... lastName

public Person(String firstName, String lastName) {
this.firstName = firstName;
...


and then



class Patient extends Person {
private final int patientID;

public Patient(int patientID, String firstName, String lastName) {
super(firstName, lastName);
this.patientID = patientID;
)


Why is that better: names and IDs don't change (normally). There is no point in having getters for them. You create your object once, and then that data is fixed! There is also no point in having that default constructor in Person. A person with empty names doesn't make sense. Thus: don't create a class that allows you to create "invalid" objects. Your classes model reality. There are no real people without names!



And one other hint: use @Override when overriding methods, so that the compiler can tell you when you get something wrong!






share|improve this answer































    0














    if the problem is to output the name in main when you call the toString method from the Patient class, I think the code bellow will help you.



    have you tried to construct the Patient object like this?



        public static void main(String args) {

    Patient p = new Patient();
    System.out.println(p.toString());

    }





    share|improve this answer































      0














      Actually I do not see problem in your code.



      Person person = new Person();
      person.setName("aa", "bb");
      System.out.println(person); // aa bb

      Patient patient = new Patient();
      patient.setName("cc", "dd");
      System.out.println(patient); // Patient Name: cc dd


      I think that you set name wrong pr use not correct reference. Check it.






      share|improve this answer





























        0














        You don't have any constructor for your PATIENT subclass. You don't set any firstName or lastName to any patient.
        To keep familiar constructor as you used in your parent class, tru to use:



        public Patient() {
        super("default_firstName", "default_lastName");
        this.patientID = 0;
        this.patientAge = 0;
        }
        public Patient(String firstName, String lastName, int patientAge) {
        super(firstName, lastName);
        this.patientID = 0; //can be implemented some method for automatically setting numbers
        this.patientAge = patientAge;
        }


        This way you always get firstName and lastName even if constructor will be called empty.
        According to you toString method, it's correct and it call super class method:



        @Override
        public String toString()
        {
        return("Patient name is "+super.toString());
        }


        But notice that you return STRING value so to make it visible on the screen remember to use:



        System.out.println(patient.toString());


        Then it will be visible :)






        share|improve this answer





























          0














          I have added some comments and code in your Person class that should fix your issues.



          public class Person {
          private String firstName; //store the first name
          private String lastName; //sore the last name

          //initialize firstName and lastName to an empty string
          public Person() {
          firstName = "";
          lastName = "";
          }

          //set firstname and lastname according to the parameters.
          public Person(String first, String last) {
          //setName(first, last); remove this crap.
          // Use the contructor properly when initialize your person object. Like this:
          this.firstName = first;
          this.lastName = last;
          }

          //method to output the first name and last name
          @Override
          public String toString() {
          return (firstName + " " + lastName);
          }

          //method to set firstName and lastName according to the paramters
          public void setName(String first, String last) {
          //
          firstName = first;
          lastName = last;
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381486%2ftrying-to-call-superclass-method-in-subclass%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Here:



            public Person()
            {
            firstName = "";
            lastName = "";
            }


            Your subclass is missing a reasonable call to a super class constructor. So when you instantiate your Patient objects, the above constructor is used, and all patients end up with "" as first and last name!



            When you create a Patient, then a patient should have a name, too! But your constructor in Patient only sets the Patient related fields. And implicitly, the default super constructor is called. Therefore the Person fields are all set to be empty strings!



            A much better approach would look like this:



            class Person {
            private final String firstName;
            ... lastName

            public Person(String firstName, String lastName) {
            this.firstName = firstName;
            ...


            and then



            class Patient extends Person {
            private final int patientID;

            public Patient(int patientID, String firstName, String lastName) {
            super(firstName, lastName);
            this.patientID = patientID;
            )


            Why is that better: names and IDs don't change (normally). There is no point in having getters for them. You create your object once, and then that data is fixed! There is also no point in having that default constructor in Person. A person with empty names doesn't make sense. Thus: don't create a class that allows you to create "invalid" objects. Your classes model reality. There are no real people without names!



            And one other hint: use @Override when overriding methods, so that the compiler can tell you when you get something wrong!






            share|improve this answer




























              1














              Here:



              public Person()
              {
              firstName = "";
              lastName = "";
              }


              Your subclass is missing a reasonable call to a super class constructor. So when you instantiate your Patient objects, the above constructor is used, and all patients end up with "" as first and last name!



              When you create a Patient, then a patient should have a name, too! But your constructor in Patient only sets the Patient related fields. And implicitly, the default super constructor is called. Therefore the Person fields are all set to be empty strings!



              A much better approach would look like this:



              class Person {
              private final String firstName;
              ... lastName

              public Person(String firstName, String lastName) {
              this.firstName = firstName;
              ...


              and then



              class Patient extends Person {
              private final int patientID;

              public Patient(int patientID, String firstName, String lastName) {
              super(firstName, lastName);
              this.patientID = patientID;
              )


              Why is that better: names and IDs don't change (normally). There is no point in having getters for them. You create your object once, and then that data is fixed! There is also no point in having that default constructor in Person. A person with empty names doesn't make sense. Thus: don't create a class that allows you to create "invalid" objects. Your classes model reality. There are no real people without names!



              And one other hint: use @Override when overriding methods, so that the compiler can tell you when you get something wrong!






              share|improve this answer


























                1












                1








                1






                Here:



                public Person()
                {
                firstName = "";
                lastName = "";
                }


                Your subclass is missing a reasonable call to a super class constructor. So when you instantiate your Patient objects, the above constructor is used, and all patients end up with "" as first and last name!



                When you create a Patient, then a patient should have a name, too! But your constructor in Patient only sets the Patient related fields. And implicitly, the default super constructor is called. Therefore the Person fields are all set to be empty strings!



                A much better approach would look like this:



                class Person {
                private final String firstName;
                ... lastName

                public Person(String firstName, String lastName) {
                this.firstName = firstName;
                ...


                and then



                class Patient extends Person {
                private final int patientID;

                public Patient(int patientID, String firstName, String lastName) {
                super(firstName, lastName);
                this.patientID = patientID;
                )


                Why is that better: names and IDs don't change (normally). There is no point in having getters for them. You create your object once, and then that data is fixed! There is also no point in having that default constructor in Person. A person with empty names doesn't make sense. Thus: don't create a class that allows you to create "invalid" objects. Your classes model reality. There are no real people without names!



                And one other hint: use @Override when overriding methods, so that the compiler can tell you when you get something wrong!






                share|improve this answer














                Here:



                public Person()
                {
                firstName = "";
                lastName = "";
                }


                Your subclass is missing a reasonable call to a super class constructor. So when you instantiate your Patient objects, the above constructor is used, and all patients end up with "" as first and last name!



                When you create a Patient, then a patient should have a name, too! But your constructor in Patient only sets the Patient related fields. And implicitly, the default super constructor is called. Therefore the Person fields are all set to be empty strings!



                A much better approach would look like this:



                class Person {
                private final String firstName;
                ... lastName

                public Person(String firstName, String lastName) {
                this.firstName = firstName;
                ...


                and then



                class Patient extends Person {
                private final int patientID;

                public Patient(int patientID, String firstName, String lastName) {
                super(firstName, lastName);
                this.patientID = patientID;
                )


                Why is that better: names and IDs don't change (normally). There is no point in having getters for them. You create your object once, and then that data is fixed! There is also no point in having that default constructor in Person. A person with empty names doesn't make sense. Thus: don't create a class that allows you to create "invalid" objects. Your classes model reality. There are no real people without names!



                And one other hint: use @Override when overriding methods, so that the compiler can tell you when you get something wrong!







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 '18 at 19:57

























                answered Nov 19 '18 at 19:52









                GhostCatGhostCat

                88.5k1685146




                88.5k1685146

























                    0














                    if the problem is to output the name in main when you call the toString method from the Patient class, I think the code bellow will help you.



                    have you tried to construct the Patient object like this?



                        public static void main(String args) {

                    Patient p = new Patient();
                    System.out.println(p.toString());

                    }





                    share|improve this answer




























                      0














                      if the problem is to output the name in main when you call the toString method from the Patient class, I think the code bellow will help you.



                      have you tried to construct the Patient object like this?



                          public static void main(String args) {

                      Patient p = new Patient();
                      System.out.println(p.toString());

                      }





                      share|improve this answer


























                        0












                        0








                        0






                        if the problem is to output the name in main when you call the toString method from the Patient class, I think the code bellow will help you.



                        have you tried to construct the Patient object like this?



                            public static void main(String args) {

                        Patient p = new Patient();
                        System.out.println(p.toString());

                        }





                        share|improve this answer














                        if the problem is to output the name in main when you call the toString method from the Patient class, I think the code bellow will help you.



                        have you tried to construct the Patient object like this?



                            public static void main(String args) {

                        Patient p = new Patient();
                        System.out.println(p.toString());

                        }






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 19 '18 at 20:12

























                        answered Nov 19 '18 at 20:04









                        Fernando TanakaFernando Tanaka

                        163




                        163























                            0














                            Actually I do not see problem in your code.



                            Person person = new Person();
                            person.setName("aa", "bb");
                            System.out.println(person); // aa bb

                            Patient patient = new Patient();
                            patient.setName("cc", "dd");
                            System.out.println(patient); // Patient Name: cc dd


                            I think that you set name wrong pr use not correct reference. Check it.






                            share|improve this answer


























                              0














                              Actually I do not see problem in your code.



                              Person person = new Person();
                              person.setName("aa", "bb");
                              System.out.println(person); // aa bb

                              Patient patient = new Patient();
                              patient.setName("cc", "dd");
                              System.out.println(patient); // Patient Name: cc dd


                              I think that you set name wrong pr use not correct reference. Check it.






                              share|improve this answer
























                                0












                                0








                                0






                                Actually I do not see problem in your code.



                                Person person = new Person();
                                person.setName("aa", "bb");
                                System.out.println(person); // aa bb

                                Patient patient = new Patient();
                                patient.setName("cc", "dd");
                                System.out.println(patient); // Patient Name: cc dd


                                I think that you set name wrong pr use not correct reference. Check it.






                                share|improve this answer












                                Actually I do not see problem in your code.



                                Person person = new Person();
                                person.setName("aa", "bb");
                                System.out.println(person); // aa bb

                                Patient patient = new Patient();
                                patient.setName("cc", "dd");
                                System.out.println(patient); // Patient Name: cc dd


                                I think that you set name wrong pr use not correct reference. Check it.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 19 '18 at 20:19









                                oleg.cherednikoleg.cherednik

                                5,95321017




                                5,95321017























                                    0














                                    You don't have any constructor for your PATIENT subclass. You don't set any firstName or lastName to any patient.
                                    To keep familiar constructor as you used in your parent class, tru to use:



                                    public Patient() {
                                    super("default_firstName", "default_lastName");
                                    this.patientID = 0;
                                    this.patientAge = 0;
                                    }
                                    public Patient(String firstName, String lastName, int patientAge) {
                                    super(firstName, lastName);
                                    this.patientID = 0; //can be implemented some method for automatically setting numbers
                                    this.patientAge = patientAge;
                                    }


                                    This way you always get firstName and lastName even if constructor will be called empty.
                                    According to you toString method, it's correct and it call super class method:



                                    @Override
                                    public String toString()
                                    {
                                    return("Patient name is "+super.toString());
                                    }


                                    But notice that you return STRING value so to make it visible on the screen remember to use:



                                    System.out.println(patient.toString());


                                    Then it will be visible :)






                                    share|improve this answer


























                                      0














                                      You don't have any constructor for your PATIENT subclass. You don't set any firstName or lastName to any patient.
                                      To keep familiar constructor as you used in your parent class, tru to use:



                                      public Patient() {
                                      super("default_firstName", "default_lastName");
                                      this.patientID = 0;
                                      this.patientAge = 0;
                                      }
                                      public Patient(String firstName, String lastName, int patientAge) {
                                      super(firstName, lastName);
                                      this.patientID = 0; //can be implemented some method for automatically setting numbers
                                      this.patientAge = patientAge;
                                      }


                                      This way you always get firstName and lastName even if constructor will be called empty.
                                      According to you toString method, it's correct and it call super class method:



                                      @Override
                                      public String toString()
                                      {
                                      return("Patient name is "+super.toString());
                                      }


                                      But notice that you return STRING value so to make it visible on the screen remember to use:



                                      System.out.println(patient.toString());


                                      Then it will be visible :)






                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        You don't have any constructor for your PATIENT subclass. You don't set any firstName or lastName to any patient.
                                        To keep familiar constructor as you used in your parent class, tru to use:



                                        public Patient() {
                                        super("default_firstName", "default_lastName");
                                        this.patientID = 0;
                                        this.patientAge = 0;
                                        }
                                        public Patient(String firstName, String lastName, int patientAge) {
                                        super(firstName, lastName);
                                        this.patientID = 0; //can be implemented some method for automatically setting numbers
                                        this.patientAge = patientAge;
                                        }


                                        This way you always get firstName and lastName even if constructor will be called empty.
                                        According to you toString method, it's correct and it call super class method:



                                        @Override
                                        public String toString()
                                        {
                                        return("Patient name is "+super.toString());
                                        }


                                        But notice that you return STRING value so to make it visible on the screen remember to use:



                                        System.out.println(patient.toString());


                                        Then it will be visible :)






                                        share|improve this answer












                                        You don't have any constructor for your PATIENT subclass. You don't set any firstName or lastName to any patient.
                                        To keep familiar constructor as you used in your parent class, tru to use:



                                        public Patient() {
                                        super("default_firstName", "default_lastName");
                                        this.patientID = 0;
                                        this.patientAge = 0;
                                        }
                                        public Patient(String firstName, String lastName, int patientAge) {
                                        super(firstName, lastName);
                                        this.patientID = 0; //can be implemented some method for automatically setting numbers
                                        this.patientAge = patientAge;
                                        }


                                        This way you always get firstName and lastName even if constructor will be called empty.
                                        According to you toString method, it's correct and it call super class method:



                                        @Override
                                        public String toString()
                                        {
                                        return("Patient name is "+super.toString());
                                        }


                                        But notice that you return STRING value so to make it visible on the screen remember to use:



                                        System.out.println(patient.toString());


                                        Then it will be visible :)







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 19 '18 at 20:37









                                        EnGoPyEnGoPy

                                        12




                                        12























                                            0














                                            I have added some comments and code in your Person class that should fix your issues.



                                            public class Person {
                                            private String firstName; //store the first name
                                            private String lastName; //sore the last name

                                            //initialize firstName and lastName to an empty string
                                            public Person() {
                                            firstName = "";
                                            lastName = "";
                                            }

                                            //set firstname and lastname according to the parameters.
                                            public Person(String first, String last) {
                                            //setName(first, last); remove this crap.
                                            // Use the contructor properly when initialize your person object. Like this:
                                            this.firstName = first;
                                            this.lastName = last;
                                            }

                                            //method to output the first name and last name
                                            @Override
                                            public String toString() {
                                            return (firstName + " " + lastName);
                                            }

                                            //method to set firstName and lastName according to the paramters
                                            public void setName(String first, String last) {
                                            //
                                            firstName = first;
                                            lastName = last;
                                            }
                                            }





                                            share|improve this answer




























                                              0














                                              I have added some comments and code in your Person class that should fix your issues.



                                              public class Person {
                                              private String firstName; //store the first name
                                              private String lastName; //sore the last name

                                              //initialize firstName and lastName to an empty string
                                              public Person() {
                                              firstName = "";
                                              lastName = "";
                                              }

                                              //set firstname and lastname according to the parameters.
                                              public Person(String first, String last) {
                                              //setName(first, last); remove this crap.
                                              // Use the contructor properly when initialize your person object. Like this:
                                              this.firstName = first;
                                              this.lastName = last;
                                              }

                                              //method to output the first name and last name
                                              @Override
                                              public String toString() {
                                              return (firstName + " " + lastName);
                                              }

                                              //method to set firstName and lastName according to the paramters
                                              public void setName(String first, String last) {
                                              //
                                              firstName = first;
                                              lastName = last;
                                              }
                                              }





                                              share|improve this answer


























                                                0












                                                0








                                                0






                                                I have added some comments and code in your Person class that should fix your issues.



                                                public class Person {
                                                private String firstName; //store the first name
                                                private String lastName; //sore the last name

                                                //initialize firstName and lastName to an empty string
                                                public Person() {
                                                firstName = "";
                                                lastName = "";
                                                }

                                                //set firstname and lastname according to the parameters.
                                                public Person(String first, String last) {
                                                //setName(first, last); remove this crap.
                                                // Use the contructor properly when initialize your person object. Like this:
                                                this.firstName = first;
                                                this.lastName = last;
                                                }

                                                //method to output the first name and last name
                                                @Override
                                                public String toString() {
                                                return (firstName + " " + lastName);
                                                }

                                                //method to set firstName and lastName according to the paramters
                                                public void setName(String first, String last) {
                                                //
                                                firstName = first;
                                                lastName = last;
                                                }
                                                }





                                                share|improve this answer














                                                I have added some comments and code in your Person class that should fix your issues.



                                                public class Person {
                                                private String firstName; //store the first name
                                                private String lastName; //sore the last name

                                                //initialize firstName and lastName to an empty string
                                                public Person() {
                                                firstName = "";
                                                lastName = "";
                                                }

                                                //set firstname and lastname according to the parameters.
                                                public Person(String first, String last) {
                                                //setName(first, last); remove this crap.
                                                // Use the contructor properly when initialize your person object. Like this:
                                                this.firstName = first;
                                                this.lastName = last;
                                                }

                                                //method to output the first name and last name
                                                @Override
                                                public String toString() {
                                                return (firstName + " " + lastName);
                                                }

                                                //method to set firstName and lastName according to the paramters
                                                public void setName(String first, String last) {
                                                //
                                                firstName = first;
                                                lastName = last;
                                                }
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 19 '18 at 22:14

























                                                answered Nov 19 '18 at 19:53









                                                DanielDaniel

                                                38115




                                                38115






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381486%2ftrying-to-call-superclass-method-in-subclass%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    MongoDB - Not Authorized To Execute Command

                                                    Npm cannot find a required file even through it is in the searched directory

                                                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith