I'm tring to fill in an arraylist. I have three classes. First class is called Animals and it is an abstract class. There I've set two fields for age and name and an abstract method for the sound of the current animal. I've made two other classes which are called Dog and Frog. For every animal I set age, name and animal's sound (For dog BAU BAU... for example :D). The problem is that I can't fill the arraylist with information for every animal that a set. I will copy the classes here in the theme.
public abstract class Animals {
public int age = 0;
public String name = null;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract String sound();
}`
public class Dog extends Animals {
public Dog(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String sound() {
return ("Bau Bau" + "Kucheto e na " + this.age + " godini. I se kazva " + this.name + ".");
}
}
public class Frog extends Animals {
public Frog(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String sound() {
return ("Kva Kva" + "Zajabate e na " + this.age + " godini. I se kazva " + this.name + ".");
}
}
public class Program {
public static void main(String[] args) {
ArrayList<Animals> animals = new ArrayList<>();
Dog dog = new Dog(12, "Kiro");
Frog frog = new Frog(44, "Nacho");
animals.add(dog.sound());
}
}
Aucun commentaire:
Enregistrer un commentaire