-3

This output make me confused.
Present Code First:

public class Animal{
    int age;
    public Animal(int age){
        this.age = 0;
    } 
    public int getAge(){
        return age;
    }
}

public class Wolf extends Animal{
    int age;
    Wolf(int age){  
        super(age);
        this.age = age;
    }
}

public class MainTest {
    public static void main(String[] args){
        Wolf wolfExample = new Wolf(2) ;        
        System.out.println("Age = " + wolfExample.getAge());
    }
}

The output is:

0

My expected output is:

2

through debug this program it seemed that wolfExample.getAge() return the age of its parent not itself, why? base on polymorphism here should return the age of wolf,
I will appreciate if you can give me some guide.

Note: Currently, I find inheritance is far difficult than what I think before.

Eran
  • 387,369
  • 54
  • 702
  • 768
liushui
  • 27
  • 4

2 Answers2

1

Polymorphism exists for methods, not for fields. Therefore getAge(), which is only implemented in the Animal class, returns the member of the Animal class.

If you override getAge() in the Wolf class, i.e. add a

@Override
public int getAge() {
    return age;
}

you'll get the value of the Wolf class member.

That said, it doesn't make sense to have an age member in both the base class and sub-class. If it's a property common to all Animals, it should only be in the Animal class.

So your Wolf class will become :

public class Wolf extends Animal {
    Wolf(int age) {  
        super(age);
    }
}

And your Animal constructor doesn't make sense. You should assign the passed age argument to the member of the class :

public Animal(int age) {
    this.age = age;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • In order to distinguish the getAge() method return the age of Animal or Wolf, I make age zero deliberately in animal constructor. I am interesting in your saying **override**, that is to say,*if i dont override this method the variable in method are all belong to parent class although the child class have this method through inheritance?* – nail fei Nov 28 '16 at 07:44
  • @cainiaofei The child class has the getAge() method through inheritance, but the method is defined in the parent class, and therefore sees the age member of the parent class. If you override the method in the child class, the getAge() method in the child class will see the age member of the child class, which will hide the age member of the parent class. – Eran Nov 28 '16 at 07:48
  • @Eran if I **override** the method I know there is a method in child class and based on **Polymorphism** jvm will invoke the method of child and return 2. **But** without **override** the child class have a getAge method too. Why jvm dont invoke the child's method. I think if *use override, child has a method of its own otherwise the method is belong to parent the child only has a authority to use it*. I dont know whether my thought is true? – liushui Nov 28 '16 at 08:03
  • Yes, without overriding the child class has a getAge() method whose implementation is in the parent class. `I think if use override, child has a method of its own otherwise the method is belong to parent the child only has a authority to use it` - I don't know what you mean by that. The child class has the method either way (as long as it's not defined as private in the parent, which would prevent the child from accessing it), regardless if it overrides it or not. – Eran Nov 28 '16 at 08:26
  • Also note: polymorphism doesn't apply to static methods either. – Peter Lawrey Nov 28 '16 at 13:41
0

You don't need "int age" in Wolf class. By commenting it out, you may get your desired output.

public class Animal{
    int age;
    public Animal(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
}
public class Wolf extends Animal{
    //int age;
    Wolf(int age){
        super(age);
        //this.age = age;
    }
}
Vasu G
  • 13
  • 2