Hi, I am having a problem overriding methods in a subclass when they contain private methods from the master class. The is the master:

package blabla;

public class Person
{
    private String firstName;
    private String secondName;
    private String familyName;

    public Person()
    {
        firstName = "";
        secondName = "";
        familyName = "";
    }

    /**
     * Parameter constructor
     */

    public Person(String firstName, String secondName, String familyName)
    {
        this.firstName = firstName;
        this.secondName = secondName;
        this.familyName = familyName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String setFName)
    {
        firstName = setFName;
    }

    public String getSecondName()
    {
        return secondName;
    }

    public void setSecondName(String setSName)
    {
        secondName = setSName;
    }

    public String getFamilyName()
    {
        return familyName;
    }

    public void setFamilyName(String setFaName)
    {
        familyName = setFaName;
    }

    public static void testClassMethod()
    {
        System.out.println("The class method in Person");
    }

    public void print()
    {
        String first = getFirstName();
        String second = getSecondName();
        String family = getFamilyName();
        System.out.println(first + " " + second + " " + family);
    }
}

And this is the sub:

package blabla;

public class Student extends Person
{
    private int facultyN;

    public Student()
    {
        super();
        facultyN = 0;
    }

    public Student(String firstName, String secondName, String familyName, int facultyN)
    {
        super(firstName, secondName, familyName);
        this.facultyN = facultyN;
    }

    public int getFacultyN()
    {
        return facultyN;
    }

    public void setFacultyN(int setFN)
    {
        facultyN = setFN;
    }

    /*
     * Overriding get/set methods
     */
    @Override
    public String getFirstName()
    {
        return firstName;
    }

    @Override
    public void setFirstName(String setFName)
    {
        firstName = setFName;
    }

    @Override
    public String getSecondName()
    {
        return secondName;
    }

    @Override
    public void setSecondName(String setSName)
    {
        secondName = setSName;
    }

    @Override
    public String getFamilyName()
    {
        return familyName;
    }

    @Override
    public void setFamilyName(String setFaName)
    {
        familyName = setFaName;
    }

    @Override
    public void print()
    {
        super.print();
        int faculty = getFacultyN();
        System.out.println("Faculty number: " + faculty);
    }
}

Typing "protected" instead of "private" before the three String variables firstName, secondName and familyName makes it working, however what is the other solution when I leave them "private"? What should I type where I am overriding them in the subclass? Thanks.

Edited 3 Years Ago by XerX

Why are you overriding those methods? What's wrong with just inheriting them? Your overides look just like the originals anyway.

Edited 3 Years Ago by JamesCherrill

Oh, yeah, it worked. Probably I haven't understood my task.

Edited 3 Years Ago by XerX: tested

Yes. That's what inheritance is all about. If your class extends Person then it automatically gets all the (non-private) variables and methods of Person.

This question has already been answered. Start a new discussion instead.