complete the JAVA code: Next, I\'d like to illustrate how inheritance works. Sup
ID: 3819416 • Letter: C
Question
complete the JAVA code:
Next, I'd like to illustrate how inheritance works. Suppose we have an Animal class that has instance variables for height and weight, an argument constructor getters and setters, and a toString method. If we want to create Fish and Bird subclasses that contain the following information: Bird instance variable: canFly (= false for penguins) Fish instance variable needsAir (= true for whales) both argument constructor, getter, setter, toString here's what they'd look like: BirdExplanation / Answer
public class Animal {
double weight;
int height;
Animal(int height, double weight){
this.height = height;
this.weight = weight;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String toString(){
return "Weight is "+this.getWeight()+" Height is "+this.getHeight();
}
}
public class Bird extends Animal{
boolean canFly;
Bird(int height, double weight, boolean canFly){
super(height,weight);
this.canFly = canFly;
}
public boolean isCanFly() {
return canFly;
}
public void setCanFly(boolean canFly) {
this.canFly = canFly;
}
public String toString(){
return super.toString()+" canFly: "+this.isCanFly();
}
}
public class Fish extends Animal{
boolean needAir;
public boolean isNeedAir() {
return needAir;
}
public void setNeedAir(boolean needAir) {
this.needAir = needAir;
}
Fish(int height, double weight, boolean needsAir){
super(height,weight);
this.needAir = needsAir;
}
public String toString(){
return super.toString()+" Need Air: "+this.isNeedAir();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.