I have a situation
public class Animal
{
String noise;
public String makeNoise()
{
return noise;
}
}
Then there will be a subclass with the concrete definition of the noise.
public class Dog extends Animal{
String noise = "woof";
}
also
public class Cat extends Animal{
String noise = "meow";
}
What I want to do is
Animal cat = new Cat();
cat.makeNoise(); // This will be 'meow'
and
Animal dog = new Dog();
dog.makeNoise(); // This will be 'woof'
Basically, I don't want to repeat the makeNoise() method when I create an animal. However, this will not work. (Noise is an empty string)
I could use a static object like
static String NoiseDog = "woof"
static String NoiseCat = "meow"
but then again I have to write the makeNoise() method for each animal. Is there a better way to architect this?