The main purpose of an Object reference by itself is to tell Java to keep the Object around. Otherwise, the memory used in that Object will go away (reclaimed by the Garbage Collector). And, with the reference, you can trigger actions on the Animal
(like simon.eat()
and simon.sleep()
). Without the reference, you couldn't do this. Anything you work on in any program must stay around by having some reference to it (like you did above).
The Object reference between Animal
and Cat
shows that "I can treat Cat, Dog, Elephant, Eagle, Moose, etc. any animal that is an Animal
like an Animal." (This is called polymorphism.)
So, an Animal
can have actions like eat()
and sleep()
because all Animal
s can eat()
and sleep()
. But, an Eagle
can also fly()
. So, Eagle
has the fly()
action defined. And, you wouldn't put the fly()
action in Animal
because not all Animal
s can fly()
.