Question
java question on inheritance program: This was the problem given to me, ignore the first sentence. I was given the following code but there is
java question on inheritance program: This was the problem given to me, ignore the first sentence. I was given the following code but there is no mention of penguins or whales in the code:
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(); } } Thanks for the help!
"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, toStringStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started