This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is calling toString inside a toString a good idea?  RSS feed

 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've recently just learned inheritance in my java class and I decide to practice inheritance by creating a test program. Here is what it look like.

Person.java



Student.java



In my last line of code, I'm invoking the toString method of my super class because in my Student constructor, you can see that I do super(name, yearOfBirth). These are instance variables that are in my super class and I can't access them because they are private. Is this a good idea? Does my program show that I'm using the inheritance concept correctly?
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfectly acceptable.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Perfectly acceptable.

And is my program using inheritance properly or is there something I can add to it if I'm not?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your program is using inheritance properly.

One thing that's important to remember about inheritance is that it means that there is an "is a" relationship from the subclass to the superclass: a Student is a Person.

When you design classes and you find that you can't say "an A is a B", then A should not be a subclass of B.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Yes, your program is using inheritance properly.

One thing that's important to remember about inheritance is that it means that there is an "is a" relationship from the subclass to the superclass: a Student is a Person.

When you design classes and you find that you can't say "an A is a B", then A should not be a subclass of B.


Thank you for that example.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I feel I should add, though - while completely acceptable - overriding a method just to call its superclass equivalent is completely redundant, seeing as this is what happens if you do not override the method at all.
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole Kristian Sandum wrote:One thing I feel I should add, though - while completely acceptable - overriding a method just to call its superclass equivalent is completely redundant, seeing as this is what happens if you do not override the method at all.


From the design point of view you're correct. But I have found it useful to do just that in this case: sometimes you can't put a debugging breakpoint in the method of the superclass, but by doing the redundant override you can put a breakpoint in your overridden method. I've done this in the past.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!