Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call super.toString in Subclasses #24

Closed
Clashsoft opened this issue Jan 6, 2020 · 1 comment
Closed

Call super.toString in Subclasses #24

Clashsoft opened this issue Jan 6, 2020 · 1 comment
Assignees
Labels
enhancement Changes to existing features
Milestone

Comments

@Clashsoft
Copy link
Member

Classes with a super class should call super.toString() in their own toString implementation.

Example:

class Person {
   String name;

   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append(' ').append(this.name);
      return builder.substring(1);
   }
}

class Student extends Person {
   int studID;

   public String toString() {
      StringBuilder builder = new StringBuilder(super.toString()); // <-- super call
      builder.append(' ').append(this.name);
      return builder.toString(); // not using substring(1), since the builder no starts with space
   }
}
@Clashsoft Clashsoft added the enhancement Changes to existing features label Jan 6, 2020
@Clashsoft Clashsoft added this to the v1.2.0 milestone Jan 6, 2020
@Clashsoft Clashsoft self-assigned this Jan 6, 2020
Clashsoft added a commit that referenced this issue Jan 6, 2020
* `toString` implementations now include `super.toString()` if a superclass is set. #24

Closes #24
@azuendorf
Copy link
Contributor

Good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Changes to existing features
Projects
None yet
Development

No branches or pull requests

2 participants