I'm doing a lesson that is supposed to demonstrate inheritance using the song Old MacDonald; there's an animal interface, a farm class that implements each animal object.. you get the idea. I've got two problems.
First,
The animal has a type and a sound, for example, a chick is
public class Chick implements Animal{
private String myType;
private String mySound;
Chick(){
myType = "chick";
mySound = "chick";
}
public String getSound(){
return mySound;
}
public String getType(){
return myType;
}
}
I'm supposed to create a second constructor with a flag that says whether a chick is childish and returns the sound "cheep" or is adult and returns "cluck" so that there's an equal change of getSound() returning either. I have no idea how to do this. I considered adding a boolean to the constructor, but that won't work because the farm class can only create a new Chick(); I realize you're not just going to hand me an answer, but I don't even know where to start.
The other issue is that I'm supposed to create a NamedCow class that, as you probably guessed, gives the cow a name. I'm supposed to be able to add a named cow to an arraylist by typing
myFarm.add(new Cow("Elsie"));
. I can't just add a getName method to the cow class, namedCow has to be a new class. I figure okay, simple,
public class NamedCow extends Cow{
private String myName;
NamedCow(String name){
myName = name;
}
public String getName(){
return myName;
}
}
Obviously that doesn't work. I really don't know what to do, and any help is greatly appreciated.