JAVA Below is a source code to define a Dog class in the Java programming langua
ID: 3875875 • Letter: J
Question
JAVA
Below is a source code to define a Dog class in the Java programming language. Examine the code and answer the questions below:
public class Dog extends Animal {
public int hairLength;
public Dog() {
}
public Dog(int age, String gender, int hairLength) {
super(age, gender);
this.hairLength = hairLength;
}
}
We use a String to represent gender in the Animal Class. What is wrong with this. What is the correct way of representing fixed sets of named values?
Explanation / Answer
The main issue with it that from the Animal class the gender wont be passed while using super.So the best way would be to assign the name directly.
like
this.gender=gender;
or you can you use the values of the base class as it is a public inheritence and assign it directly.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.