Results 1 to 6 of 6
  1. #1
    Xeal Rebad is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Inherited method that returns string

    Hey i was wondering im still a little confused on how inherited methods work?
    if i have two classes as follows

    Java Code:
    public class animal {
           string description
           
           public animal (int age, int size, String color){
                  //Do constructor stuff
           }
    
           public toString(){
                  return description;
           }
    }
    and

    Java Code:
    public class cat extends animal {
           string description
           
           public cat (int age, int size, String color, string description){
                  super(age);
                  super(size);
                  super(color);
                  //Do constructor stuff
           }
    
    }
    my question is how can i get a cat object to return the description, because i know that it should have inherited the .toString() class from animal, but i believe this is still pointing to the description in animal and not the description in cat, any one who can help me out with this whole inheritance thing would really be appreciated thanks guys :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    toString is inherited from object, it also requires a return type.
    Java Code:
    public toString()
    is wrong it should be
    Java Code:
    public String toString()
    If you want cat to have it's own unique toString, simply add a toString method which does something different for cat. You may want to complete the animal constructor and test everything so you can get a better understanding.

  3. #3
    Xeal Rebad is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    ah yea woops my bad about the not having a return type. I do in my actual code... but the thing im not getting is that Inheritance is meant to make it so that i dont have to rewrite some code that is used in all objects. The toString is doing the same thing in both pieces of code is there a way that i can tell it to return the description that is relative to the corresponding class? also im realising i think that there is something wrong with my understanding of how to create the inherited object. How do i make it do everything that the animal class is doing (eg set the age, size, color) and then one extra thing (eg set description).

    Thanks again, and i am playing around with java its just this inheritance thing is stumping me a little thanks :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Basically everything in the animal class(super class) is inherited by the cat class. So when you create an instance of cat it will have all the public methods animal has, as well as the instance variables animal has. Try creating a good constructor for the animal class, then use the cat class constructor and have it call the super constructor with the super keyword.

    Your toString method returns the description, so for the animal class it will return the animals description. For cat it will also return the description.

    Post back if you need more clarification, and perhaps visiting Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance) will be helpful.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    Your problem is that cat really shouldn't have a description attribute.
    It inherits the one from animal anyway, so why give cat its own?
    That is generally a coding bug.

    (Oh, and class names should begin with an upper case, so Animal and Cat).

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Incidentally, the Cat constructor body is incorrect - you can only call the superclass constructor once, and the call must be the first line of the constructor:
    Java Code:
    public cat (int age, int size, String color, string description){
       super(age, size, color);
    
       this.description = description;  // why isn't this done in Animal instead?
    }
    Also, you're not passing a description to set the 'description' field in the Animal constructor, although you do in the Cat constructor. If you change the Animal constructor to take a description and initialise the 'description' field, it would be more sensible. Then the Cat constructor (and other Animal subclasses) wouldn't need to set it.

Similar Threads

  1. accessing variables in a method that returns a void
    By mochajava in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 08:00 PM
  2. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 12:16 PM
  3. Replies: 2
    Last Post: 10-15-2010, 05:09 AM
  4. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:28 AM
  5. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •