-2

When I have a class Animal and when I want to create an array for this objects I write this line of code Animal[] animal = new Animal[4]. And I imagine this like this picture:

enter image description here If I have class Cat that extends Animal I can also write Cat[] cat = new Cat[4] to create an array. Also I imagine like this picture:

enter image description here But the question is what does this line Animal[] animal = new Cat[4] do.

Archer
  • 67
  • 11
  • 6
    Take the `Animal[]` pointer from your top picture and point it down to the cat array in your bottom picture. – khelwood Jul 08 '20 at 19:07
  • So do you want to say that `Animal[]` points to `Cat[]` that points to array of `Cat` in heap? – Archer Jul 08 '20 at 19:09
  • ***new*** just means you allocate memory for your array object – ΦXocę 웃 Пepeúpa ツ Jul 08 '20 at 19:12
  • `Animal[] animal` declares a variable of type animal-array, and `new Cat[4]` creates a cat array. – khelwood Jul 08 '20 at 19:14
  • "So do you want to say that Animal[] points to Cat[] that points to array of Cat in heap?"—No, the `Animal[]` variable points to the array of Cat in the heap. The `Cat[]` variable is not involved. – khelwood Jul 08 '20 at 19:19
  • Arrays in Java are "covariant", which means `Cat[]` acts like a _subtype_ of `Animal[]`. Just like with scalar (non-array) types, where you can say `Animal a = new Cat();`, you can say `Animal[] a = new Cat[4]`. This can get you in trouble, as seen in https://stackoverflow.com/q/28570877/611819. – dnault Jul 08 '20 at 19:29

1 Answers1

1

The 'new' keyword makes your machine allocate memory for a new object.

In this case, the new object you're initializing is an array, and the array has enough space to hold 4 animal objects in it. Since Cat extends animal, cats can be here as well, but any other animal can Dogs, Pigs, so long as they extend Animal.

Since this is an Array of type animal, even though cat objects are inside of it, you'll only be able to reference the method declarations of type animal.

Before we move to an example with arrays here is an example without arrays:

public static void main(String[] args) {
        Animal myCat = new Cat();
        // prints "meow". Works because it's from an animal method  
        myCat.speak(); 

        // This doesn't work - unknown method - b/c it's not declared in animal class
        myCat.lickSelf(); 
    }

    private static class Animal {
        public void speak() {
            System.out.println("barf");
        }
    }

    private static class Cat extends Animal {
        public void speak(){
            System.out.println("meow");
        }
        public void lickSelf(){
            System.out.println("slurp");
        }
    }

Now let's move to the array stuff:

Any object extending cat can fit here, but you can only access Animal properties.

public static void main(String[] args) {
    Animal[] myCats = new Cat[4];
    myCats[0] = new Cat(); // can only put cats in here
    myCats[1] = new Cat(); // but can only reference Animal properties
    myCats[2] = new Cat();
    myCats[3] = new Animal(); // Throws exception, must be a cat. 

    myCats[1].speak(); // prints "meow"

    // This doesn't work, even though a cat object is here. 
    //Can only reference as an Animal.
    myCats[2].lickSelf();

}

If you want to use cat methods/properties you need to do this:

Cat[] myCats = new Cat[4]

If you want to use ONLY animal properties that may be overridden, but ONLY allow cats here, use this:

Animal[] myCats = new Cat[4]

If you want to use only animal properties, but allow any type of animals in:

Animal[] myCats = new Animal[4]
hippomano
  • 79
  • 5