An inheritance question.
I have 1 superclass and 2 subclasses.
Problem: Can I use inheritance for the field age and method getAge()? What confuses me is that the field age in Dog class is fixed (15) and not a parameter. I would like to move method getAge() to the superclass but the fixed value of Dog's age confuses me. What to do when a field in one (sub)class is fixed to a value and it's not in the other. Can you still use inheritance? How to solve this?
//Super class:
public class Animal
{
private String name;
public Animal(String nam)
{
name = nam;
}
public void displayName("This animal is: " + name);
}
//Subclass 1:
public class Cat extends Animal
{
private int age;
public Cat(String nam, int a)
{
super(nam);
age = a;
}
public int getAge()
{
return age;
}
}
//Subclass 2:
public class Dog extends Animal
{
private int age;
public Dog(String nam)
{
super(nam)
age = 15;
}
public int getAge()
{
return age;
}
}
Matt
How about:
public abstract class Animal
{
private String name;
private int age;
public Animal(String nam)
{
name = nam;
}
public void displayName()
{
System.out.println("This animal is: " + name);
}
public abstract int getAgeInAnimalYears();
}
public class Cat extends Animal
{
private int age;
public Cat(String nam, int a)
{
super(nam);
age = a;
}
@Override
public int getAgeInAnimalYears()
{
return age * 9;
}
}
public class Dog extends Animal
{
private int age;
public Dog(String nam)
{
super(nam)
age = 15;
}
@Override
public int getAgeInAnimalYears()
{
return age * 7;
}
}
This uses an abstract class ( http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html ) for Animal. Note that this means you cannot instantiate Animal objects anymore, they have to be Cats or Dogs or any other subclass of Animal. I think that is a reasonable design choice, but if for some reason you need to instantiate pure Animals, you could leave those as a normal class and throw an exception instead:
public class Animal
{
private String name;
private int age;
public Animal(String nam)
{
name = nam;
}
public void displayName()
{
System.out.println("This animal is: " + name);
}
public int getAgeInAnimalYears() {
throw new UnsupportedOperationException();
}
}
To address your question about code reuse: You are already saving space by implementing displayName() only once - imagine if that were a much more complicated function that still worked the same for all animals, and also imagine three more sublasses, suddenly you save a big bunch of code. Much more attractive, however, is the following feature that inheritance allows you to do:
public class Zoo
{
private ArrayList<Animal> list = new ArrayList<Animal>();
public void addAnimal(Animal a)
{
list.add(a);
}
public void everyoneSayHello()
{
for (Animal a : list) {
a.displayName();
System.out.println("It is " + a.getAgeInAnimalYears() + \
" animal-years old!")
}
}
}
You can now add Dogs, Cats and whatever to your list, and when you call everyoneSayHello(), Polymorphism automatically calls the right getAgeInAnimalYears() for you! Without inheritance, this only works with large if/case blocks which are hard to extend. With inheritance, you just add another class that extends Animal, no need to change any other code!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With