Java inheritance - subclass method override
Java inheritance - subclass method override
I am struggling with an inheritance task in Java
I was given an Animal.java class. My homework is to create a subclass called Lion.java. One of the tasks I'm struggling with within the entire task is outputting the type of Lion it is based on the weight of the lion. This is the code for the Animal.java
public class Animal {
private int numTeeth = 0;
private boolean spots = false;
private int weight = 0;
public Animal(int numTeeth, boolean spots, int weight){
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
public int getNumTeeth(){
return numTeeth;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public boolean getSpots() {
return spots;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public static void main(String args){
Lion lion = new Lion(30, false, 80);
System.out.println(lion);
}
}
This is my code for the Lion.java class so far:
public class Lion extends Animal {
String type = "";
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
}
public String type(int weight){
super.setWeight(weight);
if(weight <= 80){
type = "Cub";
}
else if(weight <= 120){
type = "Female";
}
else{
type = "Male";
}
return type;
}
@Override
public String toString() {
String output = "Number of Teeth: " + getNumTeeth();
output += "nDoes it have spots?: " + getSpots();
output += "nHow much does it weigh: " + getWeight();
output += "nType of Lion: " + type;
return output;
The problem is the output does not return the type based on the if statement above. It's probably a very simple solution but I can't seem to figure it out.
Check with your instructor. Are you sure you are supposed to add a new method
type()
? Or are you supposed to override setWeight()
? (In other words, we can't tell what's wrong. You're either calling the methods incorrectly (in other code you haven't shown) or you didn't understand and follow the assignment. Could be either one.)– markspace
Jul 2 at 19:28
type()
setWeight()
@SotiriosDelimanolis one of the questions in the task is "Add a method in this class which sets the lion type based on it’s weight (note that the weight is a derived field from the superclass)."
– Zee Dhlomo
Jul 2 at 19:30
"Add a method" doesn't mean add a method and do nothing else. I think you were also supposed to do something else. (I'm not saying what I think it is because I think figuring it out is the whole point of the assignment.)
– markspace
Jul 2 at 19:33
That's not what I'm asking. Why do you think your
toString()
method in any way involves the type
method, and its logic?– Sotirios Delimanolis
Jul 2 at 19:34
toString()
type
2 Answers
2
In toString method, instead of type replace with type() method.
@Override
public String toString() {
String output = "Number of Teeth: " + getNumTeeth();
output += "nDoes it have spots?: " + getSpots();
output += "nHow much does it weigh: " + getWeight();
output += "nType of Lion: " + type(getWeight());
return output;
Take a good look at your Lion
constructor
Lion
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
}
This doesn't do anything for the type (your public type
method).
type
In order to set the private type
class variable you need to either call the type
method in the constructor or after the object has been created but before you call the toString
method. For example
type
type
toString
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
type(weight);
}
Note that, as pointed out in the comments, you probably would be better off handling the type
directly in the setWeight
method. You can do something like
type
setWeight
@Override
public void setWeight(int weight) {
super.setWeight(weight);
type(weight);
}
and leave the constructor alone.
Taking it one step further, you could refactor your code such that the type
method has no parameter (you've already set the weight
member).
type
weight
This will "work" but I don't think it completes the assignment. I think it's more likely the OP was supposed to override
setWeight()
. Just a guess though.– markspace
Jul 2 at 19:31
setWeight()
@markspace Yes, thanks for pointing that out. I've modified the answer.
– William Burnham
Jul 2 at 19:36
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The problem is the output does not return the type based on the if statement above Why should it?
– Sotirios Delimanolis
Jul 2 at 19:27