0

I have a class Person and its subclass Student:

public class Person {

private String name;

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

public class Student extends Person {

private int grade;

public Student(String name, int grade) {
    super(name);

    this.grade = grade;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}

public void printDescription() {
    System.out.println("Name: " + getName());
    System.out.println("Grade: " + Integer.toString(grade));
}

}

So Person have getter and setter for name property and Student have only getter and setter for its new grade property, as long as a printDescription() method. The problem is how should I call the name property in Student's printDescription() method correctly?

I implemented it like in code above considering that Student inherits getter and setter from parent class. But at my university Java teacher asks to use it like this:

public void printDescription() {
    System.out.println("Name: " + super.getName());
    System.out.println("Grade: " + Integer.toString(grade));
}

So he offers to directly call parent's getter. I think it is not the best way because in case we override name's getter in Student class, getter from Person will still be called instead.

So what approach is best in this situation to use name property?

UPD: it is important to mention that for this task there is no requirement to call specifically superclass' getter implementation, this is why I was confused by teacher's recommendation to use super.

Maria
  • 755
  • 1
  • 11
  • 29
  • `this.getName()` (or even just `getName()`) should do the trick. – Federico klez Culloca Feb 01 '19 at 08:41
  • But according to this answer https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class "this" keyword should not be used in this situation. – Maria Feb 01 '19 at 08:42
  • "in case we override name's getter in Student class, getter from Person will still be called instead" => Make the getter in the parent class _final_. It simply isn't dedicated to being overwritten. And then you can omit any `super` or `this`. – Seelenvirtuose Feb 01 '19 at 08:44
  • 2
    No, the answer says it's primarily used in those situations, not that it should not be used in other situations. I agree it's redundant, but it's certainly not *wrong*. – Federico klez Culloca Feb 01 '19 at 08:44
  • In this case, which one should be preferred - getName() or this.getName() ? – Maria Feb 01 '19 at 08:51
  • 1
    That's just preference. Whatever seems more clear to you – Federico klez Culloca Feb 01 '19 at 08:55
  • With prior Objective-C experience calling method with this.getName() looks more familiar for me. But "this" in java and "self" in ObjC works a little different when it's coming to getters and setters, and this is exactly why I'm so confused with such a simple question. – Maria Feb 01 '19 at 08:58
  • Ok, I never used ObjC so I'll have to trust you on that one :) Anyway, that's what code style guidelines are for. If you're the only person working on your code, do whatever you want or you consider more readable. If you're on a team, follow their conventions. – Federico klez Culloca Feb 01 '19 at 09:02

2 Answers2

2

You're correct that if you override the method in the subclass and you're using the super keyword then you'll invoke the method on the parent class.

In this case unless you wanted to guarantee that the method in the parent class was used then it's fine to just invoke the method without the super keyword and that way if you override the method then you get the behaviour you want in the subclass.

Old Nick
  • 995
  • 9
  • 19
  • Thanks for the answer. This is exactly what I wanted to understand about the situation. – Maria Feb 01 '19 at 08:55
0

if you extends a class , it will has all properties in its superclass. instead of calling super.getName() you can just call this.getName().

Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28
  • OP said (correctly) that the addition of a `getName` method in the subclass will change the behavior of the `printDescription` method, which is the key part of his question. Your answer does not address this. – Seelenvirtuose Feb 01 '19 at 08:46
  • 1
    @Seelenvirtuose I understood the question as the opposite of what you say, i.e. "if I override the `getName` method I want to call the subclass' one, not the superclass' one" – Federico klez Culloca Feb 01 '19 at 08:47
  • It seems, that OP is asking about the _better way_ (which in itself is somewhat opinion-based). OP also seems to be aware of the distinction between `super.getName()` and `this.getName()`. So he asks for arguments for both sides, which your answer unfortunately does not provide. He even started with `this.getName()` whereas his teacher told him to use `super.getName()`. – Seelenvirtuose Feb 01 '19 at 08:50