0

I am working on a project for my intro to java class and am working on inheritance. The problem is with the classes Mammal and Pet. When I run the driver class I get a stack overflow error on line 12 where it calls setName(inName) of Mammal. Can someone point me in the right direction?


package inlab9;


/**
 * Driver for Inlab9.
 * 
 * @author yaw 
 * @version 14 Nov 2014
 */
public class Driver
{
    public static void main(String[] args)
    {
        Pet p = new Pet("Terrance");
        p.printInfo();
        System.out.println();
    
        Mammal m = new Mammal();
        m.printInfo();
        m.setName("Roxie");
        m.printInfo();
        System.out.println();
    
        Fish f = new Fish("red");
        f.printInfo();
        f.setName("Shark");
        f.printInfo();
        System.out.println();
    
        Dog d = new Dog("Watson", "Basset");
        d.printInfo();
    }
}
package inlab9;

public class Mammal extends Pet {
    protected static String name = "Fluffy";

    public Mammal(){
        super(name);
    }

    public void setName(String inName){
        setName(inName);
    }
    public void printInfo(){
        System.out.println("The pet's name is " +name);
    }
}

package inlab9;

public class Pet {
    protected String name;

    public Pet(String name){
        this.name = name;
    }

    public void printInfo(){
        System.out.println("The pets name is " + name);
    }
    public void setName(String name){
        this.name = name;
    }
}
2
  • The implementation of setName() looks a little problematic. Commented Nov 19, 2014 at 4:15
  • Aside from the answers, there are some conceptual problem with your inheritance tree. By making Pet a superclass of Mammal for instance, you are saying that a mammal is a pet. There are tons of mammals that can't be domesticated. Also, some Fish can be pets. Therefore, it is probably best to make Pet an interface instead of a class. Remember, the scope narrows when you get down the inheritance tree. That means that a subclass is a more specific type than its superclass. This rule is not observed in your example. I know it has nothing to do with your problem, but a needed observation. Commented Nov 19, 2014 at 4:25

3 Answers 3

2

To call the super class method, you should write:

public void setName(String inName){
    super.setName(inName);
}

That said, you don't really need to implement setName in the sub-class if it only calls the super class implementation.

Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is here,in class Mammal

public void setName(String inName){
    setName(inName);//<-- recursive class
}

Try instead, if you really want to override it, else don't do it

@Override
public void setName(String inName){
    super.setName(inName);
}

1 Comment

perfect I didn't think t use super.setName. Thank you very much
0

When you call

public void setName(String inName){
   setName(inName);
}

you are recursively calling the method and there is no break to stop it, so the stack keeps incrementing until it overflows. You might want to try using the keyword super before to call the parent class' method:

super.setName(inName);

But since this is a child of a parent with this predefined method, you really don't even need this unless you want this method to do something different. That is the point of inheritance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.