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.

Recommended Answers

All 3 Replies

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.