Refactoring in Java: Duplicated attributes

Viewed 41

I am supposed to refactor duplicated attributes in Student class. I have Student and Professor classes as below. I am really confused about how to do refactoring with attributes. Should I add a new class, or made modifications in one of the classes. If so, how? I could not understand how to proceed with this to-do.

    private final String matrNr;
    private final String name;
    private final int age;
    private int semester;
    private final String email;

   
    public Student(String name, int age, String email, String matrNr, int semester) {
        this.matrNr = matrNr;
        this.name = name;
        this.age = age;
        this.semester = semester;
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getSemester() {
        return semester;
    }

    public String getMatrNr() {
        return matrNr;
    }

    public void increaseSemester(){
        semester = semester + 1;
    }
}

And the professor is a like:


    private final String persNr;
    private final String name;
    private final int age;
    private final String email;

   

    public Professor(String name, int age, String email, String persNr) {
        this.persNr = persNr;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPersNr() {
        return persNr;
    }
}

Thanks for any kind of helps!

1 Answers

Your goal is to refactor duplicated attributes in the Student and Professor classes. The way to do this is to create a parent class which defines the common attributes (like "name"), and modify Student and Professor classes to extend the common parent class. In this way, both Students and Professors can have a "name", even though you have defined "name" only once in the common parent.

Below shows how you could do this with a common "Human" parent class, how the constructors would work, and how you could define a Student-only attribute (semester).

Here is a simple version a common Human class:

  • common "Human" class
  • each Human has a "name"
  • the name is set in the constructor (so when you're creating an object) and cannot be changed later ("name" is final; also no "setHuman()")
class Human {
    private final String name;

    public Human(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Here's a simple Professor class:

  • by definition, a Professor is a Human (Professor extends Human)
  • when creating a Professor, you must specify the "name" (which is then passed to the Human constructor)
  • once you have a Professor, you can call getName() (which is defined on the Human class)
class Professor extends Human {
    public Professor(String name) {
        super(name);
    }
}

Here's a simple Student class:

  • Student is a little different - in addition to a name, it also has a "semester"
  • when creating a Student, the constructor requires a name and semester, and the Student class itself keeps track of "semester" – so it's fine to have semester defined on Student, and name defined on Human.
  • you can call getName() (defined on Human)
  • you can call getSemester() (defined on Student)
class Student extends Human {
    private final int semester;

    public Student(String name, int semester) {
        super(name);
        this.semester = semester;
    }

    public int getSemester() {
        return semester;
    }
}
Related