• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Is calling toString inside a toString a good idea?

 
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 9717
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic