0
How does the subclass Dog inherit the variable legs with the value of 4 from the superclass Animal
How does the subclass Dog inherit the variable legs with the value of 4 from the superclass Animal, when the variable legs doesn't store any value in the superclass. Where is it inheriting the 4 from? Am I missing something - I realise ive posted a comment in Q & A my mistake!
1 Respuesta
+ 1
your question is not clear.
you should specify the programming language and the code example.I had to check your profile to guess it's Java.
the value 4 is not inherited it's set to 4 in the subclass.
it inherited the property legs.
settings the value in the superclass is useless and it kills the purpose of inheritance in this example, because you could have an animal with less legs, say for example you have an ostrich as subclass, it wouldn't inherit 4.
class Animal {
protected int legs;
public void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
Dog() {
legs = 4;
}
}