0

I am learning java and polymorphism. I am using book head start with java. I was trying to experiment with them. I learned that methods you can call depends upon the reference object.So I created this code

package animals;

public class AnimalstestDrive {

    static public Animals[] myZoo = new Animals[5];
    static int zooCounter = 0;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Dog 1 test");
        Dog myDog = new Dog();
        myDog.eatFood();
        myDog.sleep();
        myDog.makeNoises();

        System.out.println("Dog 2 test");
        Animals myNewDog = new Dog();
        myNewDog.eatFood();
        myNewDog.makeNoises();

        //Set animals array
        System.out.println("Dog 3 test");
        Animals zooDog = new Dog();
        addAnimals(zooDog);

        Cat zooCat = new Cat();
        addAnimals(zooCat);

        myZoo[0].makeNoises();


    }

    public static void addAnimals(Animals a){
        if ( zooCounter < 5 ){
            myZoo[zooCounter] = a;
            zooCounter++;
        }
        else
            System.out.println("Zoo is full");
    }
}

One thing that causing me problem is that "Dog 3 test" is not showing on console. ITs working fine till Dog 2 but Dog 3 is not working.

Here is my animal class

package animals;

public abstract class Animals {
    private String Name;
    private int Size; //Size on the scale 1 to 10

    public void eatFood(){
        System.out.println("I am eating food");
    }

    public void sleep(){
        System.out.println("I am sleeping now");
    }

    abstract public void makeNoises();

}
  • 2
    Can you show the definition of `Animals`? – user253751 May 04 '15 at 05:37
  • Do u ever heard about **upcasting** & **downcasting** in Java. This is upcasting where `Dog` which is more specific animal cast to it's generalized form i.e., `Animal` – OO7 May 04 '15 at 05:40
  • ^ this isn't his problem, since he's declaring the types of his individual `Animals` to be the base type, but defining the object to be the explicit type of animal (ie: `Animals myNewDog = new Dog();`, which would work perfectly fine so long as `Animals` defines a `makeNoises()` method. My guess is, it's a typo. – Ryan J May 04 '15 at 05:42
  • Check this http://stackoverflow.com/questions/23414090/what-is-the-difference-between-up-casting-and-down-casting-with-respect-to-class – OO7 May 04 '15 at 05:42
  • 1
    I'm with immibis. We need to see the definition of `Animals`. You probably have forgotten to declare a `makeNoise()` method in that class, which is why you can't call it. – markspace May 04 '15 at 05:45
  • Why would "Dog 3 test" be printed on the console. This String never appears in the code. – JB Nizet May 04 '15 at 06:03
  • Sorry, I forgot to put that part Dog 3 test, its simple system.out.println("Dog 3 test"); – Ajay Birbal May 04 '15 at 06:07
  • What is ur expected output ? `"Dog 3 test"` is printed on console as it's a simple print statement. – OO7 May 04 '15 at 06:15
  • @OO7 I am using eclipse and its not printing it – Ajay Birbal May 04 '15 at 06:23
  • I have tried ur code in Eclipse Kepler & it's working fine. No issue. – OO7 May 04 '15 at 06:24
  • I am using stanford version of eclipse. Maybe its broken – Ajay Birbal May 04 '15 at 06:27
  • Try in some other Editor to prove ur point. – OO7 May 04 '15 at 06:29

1 Answers1

0

I think this is an typo because one place you are using myNewDog.makeNoises() and another place you are using myZoo[0].makeNoise();

So if its a typo change it to myZoo[0].makeNoises();


Regarding upcasting and downcasting see What is the difference between up-casting and down-casting with respect to class variable:

Up-casting is casting to a supertype, while downcasting is casting to a subtype.

So when you type Animals zooDog = new Dog(); its a upcasting.
and Dog newDog = (Dog)zooDog; this is a downcasting.

For more details see JLS 5.5.1. Reference Type Casting

Community
  • 1
  • 1
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100