I am doing a coding boot camp and created a Superclass "Animal" with a subclass "Lion". All the variables are private variables.
When I try and pass in the values required I get an error saying Lion cannot be resolved to a type. This is my first question ever asked on StackOverflow, so sorry if the normal convention isn't followed right.
///Animal class with subclass lion///
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
}
///Animal stats I am trying to pass in "AnimalDetails" which is a new class file///
public class AnimalStats {
public static void main(String[] args) {
Lion stats = new Lion();
stats.setMaine(true);
stats.setNumTeeth(20);
stats.setRegion("South Africa");
stats.setSpots(false);
stats.setType(2);
stats.setWeight(150);
}
}
The error I find is in / Lion stats = new Lion(); / Lion cannot be reloved to a type
Lion is not declared static
In order to construct Lion in the way you are, Lion should be a static inner class.
The declared constructor in Lion does not include params for super
Add params for the call to super, or provide default values inside the constructor implementation for numTeeth, spots, and weight when you call super.
Lion does not have a zero-argument constructor
Your code is trying to instantiate a Lion with no arguments. If you want a zero-argument constructor, add one and set defaults for all of the members.
Otherwise, call the constructor you've already defined.
Something like:
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
static class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type, int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
public static void main(String[] args) {
Lion stats = new Lion(true, "South Africa", 2, 20, false, 150);
}
}