3

I'm doing an assignment based around inheritance and I have created 2 constructors that are suppose to do different things. One constructor does not have any parameters and should produce a pre-defined value, the other constructor has 2 parameters which consist of a name and an age of types String and int. I have somehow reconfigured the two constructors so that they both do not produce what they should be. Here is the classes that these constructors are invoked in:

Animal (super class)

abstract public class Animal implements Comparable<Animal>
{
    int age;
    String name;

    Animal(String name, int age)   
    {
        this.age = age;
        this.name = name;
    } 

    Animal()
    {   
         this("newborn", 0);
    }        

    public int getAge()
    {
         return age;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    String getName()
    {
        return name;
    }
}

Carnivore

public class Carnivore extends Animal
{
    Carnivore(String name, int age)   
    {
        this.age = age;
        this.name = name;
    }

    Carnivore()
    {
        super();
    }

    @Override
    public int compareTo(Animal o)
    {
        //To change body of generated methods, choose Tools | Templates.
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

Wolf

public class Wolf extends Carnivore
{
    String name;
    int age;

    Wolf(String name, int age)   
    {  
        this.name = name;
        this.age = age;
    }

    Wolf()
    {
        super();
    }   

    String getName()
    {
        return name;
    }
}

Main method

System.out.println("************1st constructor of Wolf************");
Wolf wolfExample = new Wolf("Bob", 2) {};        
System.out.println("Name = " + wolfExample.getName());
System.out.println("Age = " + wolfExample.getAge());

System.out.println("************2nd constructor of Wolf************");    
Wolf newWolf = new Wolf();
System.out.println("Name = " + newWolf.getName());
System.out.println("Age = " + newWolf.getAge());

Actual Output

************1st constructor of Wolf************
Name = Bob
Age = 0
************2nd constructor of Wolf************
Name = null
Age = 0

Expected Output

************1st constructor of Wolf************
Name = Bob
Age = 2
************2nd constructor of Wolf************
Name = newborn
Age = 0

The ages are returning their default value and the name for the second constructor is also returning null but I'm not too sure why. This is my first time working with multiple constructors so I'm a little confused as to ow it works so any help would be much appreciated, thanks.

2
  • Please look at posting a minimal reproducible example for quicker help! Commented Nov 28, 2016 at 6:02
  • this is what you are doing this("newborn", 0); Commented Nov 28, 2016 at 6:05

1 Answer 1

3

Your base class seems correct, but you need to change your implementations.

Your Wolf and Carnivore constructors should be:

Wolf(String name, int age)   
{
    super(name, age);
}

Reason being, you are setting the local instance variables for each type, but calling getAge() method of the super class - this is getting the super's value of age, whose's value has not actually been assigned anywhere, and is given a default value of 0. This goes the same for name, which defaults to null.

You need to call super with the passed variables, and do not need to redefine them for each extended object.

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

4 Comments

ahahah, I was just rereading through everything to see why it would not have based on your last comment ;) glad to hear it, happy coding! be sure to upvote / accept helpful answers :)
know that you do not need to override getName in _Wolf_either, calling it on this object will use the method / variable value defined in the base class.
Yeah I realized the same thing when I went through that class again and just deleted that method xD Thanks for your help, I up-voted and picked yours as the best answer.
@Matt Clark The Wolf inherit Animal then it should have its own getAge method although it dont override this method,but then why wolf.getAge return the age of Animal, I cant figure out. And if I remove the filed variable age and name in Wolf and so Wolf inherit filed of Animal it will output what OP expect, maybe when inherit filed both parent and children's filed variable is the same memory? can you give me some explanation or relevant link?

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.