javamultiple-inheritancemultiple-constructors

How to correctly configure multiple constructors?


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.


Solution

  • 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.