-1

Hi I'm really new to java and having some issues. I have a question and I'm not sure how to phrase it so I'll try to give an example.

First lets say I have three classes that share the same package. First lets say I have the following Abstract class:

public abstract class Animal {
    protected String name;
    protected String type;

    public String getType() {
        return this.type;
    }
}

And the following child class

public class Lion extends Animal {
    
    public Animal (String name, String dietType) {
        this.name = name;
        this.type = dietType;

}

Then lets say I have another class that keeps track of a list of animals that shares the SAME package as Lion and Animal. This class has one method that add animals into an ArrayList it possesses. The problem is when I try to create a Lion (which is a type of animal) inside the class and add it to the ArrayList of Animals it won't add.

public class AnimalList {
    private ArrayList<Animal> animalList = new ArrayList<Animal>();

    public void addAnimal(Animal animal) {
        Lion lion1 = new Lion("Simba", "Carnivore");
        animalList.add(lion1);
    }
}

If I wanted to add a Lion into the ArrayList of animals how would I do so? Would I have to typecast the Lion into an Animal? But a Lion is already an Animal isn't it?

mjam94
  • 25
  • 4
  • 1
    What do you mean, "it won't add"? What happened when you tried? How do you know it "isn't added"? Are you sure that the method actually got called in the first place? – Karl Knechtel Nov 14 '20 at 19:58
  • 1
    Please show a *complete* example - enough code that someone else can copy and paste the entire thing, run it, and see the exact problem you are having difficulty with. – Karl Knechtel Nov 14 '20 at 19:59
  • 1
    you wrote `addAnimal` in a way, that it always will add lion Simba, but not the animal passed in as parameter. ist this intentional? – Curiosa Globunznik Nov 14 '20 at 20:00
  • @NikolaiDmitriev Oh sorry that was a mistake, but you answered my question. Thank you! – mjam94 Nov 14 '20 at 20:39

1 Answers1

0

There's just one significant problem preventing your code from being syntactically correct and runnable. You have misnamed the constructor in your Lion class. That class should be defined like this:

public class Lion extends Animal {
    
    public Lion (String name, String dietType) {
        this.name = name;
        this.type = dietType;

}

The other issue is one of intent. As @Nickolai said, you probably don't want to be adding a specific animal inside your AnimalList class, but rather be adding the Animal you passed in, so just:

public class AnimalList {
    private ArrayList<Animal> animalList = new ArrayList<Animal>();

    public void addAnimal(Animal animal) {
        animalList.add(animal);
    }
}

To complete the picture, here's a main method that will exercise your code:

public static void main(String[] args) {
    AnimalList al = new AnimalList();
    Lion lion1 = new Lion("Simba", "Carnivore");
    al.addAnimal(lion1);
}

This code basically answers your question. Yes, a Lion is already an Animal, so no type cast is needed to pass a Lion into the addAnimal method, even though it accepts an Animal as a parameter.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44