How does the subclass Dog inherit the variable legs with the value of 4 from the superclass Animal | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!

10th May 2019, 2:54 AM
Shak 240
Shak 240 - avatar
1 Answer
+ 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; } }
10th May 2019, 3:52 AM
Bahhaⵣ
Bahhaⵣ - avatar