public class Pet{
...
public abstract String sound();
}
public class Dog extends Pet{
...
public String sound(){
return "gav-gav";
}
}
public class Cat extends Pet{
...
public String sound(){
return "mao-mao";
}
}
Где-то ещё:
List<Pet> pets = new ArrayList<>();
pets.add(new Cat());
pets.add(new Dog());
for(Pet pet: pets){
System.out.println(pet.sound());
}