• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Inheritance in Java - super.toString() returns null?  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question pretty much says it all, but I tasked myself with creating a program about lemurs. There are multiple class files in this program. In the below code snippet, I have my TreeLemur.class which extends to the Lemur.class which extends to the Mammal.class. However, when I create a Tree Lemur object in the main program, it is returning null consistently from certain methods. What am I doing wrong here?

TreeLemur.class :



Lemur.class:



Mammal.class:
** I am wondering if Mammal needs to extend off of anything? I don't think it needs to, but I am running out of ideas here. :P **



Main program file:



As of yet, I'm just trying to get Tree Lemur working properly to continue with creating the other if-branches within the main program. Thank you for any help, as I am new to the idea of inheritance.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anywhere that you call the setGroomingHabits() method on Lemur. Which means grooming habits still has the default value - null.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"00" is age and weight with no separating characters.
"null" is gender because you haven't set a default value nor made a call to setGender().
Typically a field would not have a value that includes the field name. Example: origin should just contain "Madagascar" without the leading "Origin: ". Then if you want to add the field name to the output do it in toString().
In your toString() methods you are causing a bunch of String's to be created unnecessarily.

Would be better written with a StringBuilder.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will have some formatting problems.

You can define a small helper method to make the output nicer and more consistent:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!