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. java question on inheritance program: This was the problem given to me, 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, toString

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!