public static void feedPet(Pet p)
{
/* implementation not shown */
}
public static void main(String[] args)
{
Cat c = new Cat(Color.BLACK);
feedPet(c);
}
Immediately after the call to feedPet
, there are 2 variables/references to the same Cat
object. The variable c
, which exists only in main
, is of type Cat
. The variable p
, which exists only in feedPet
, is of type Pet
.
The feedPet
method can accept an argument of type Pet
or any subclass of Pet
. It is not necessary to cast c
to a Pet
prior to the call. Any object to which c
can refer can also be refered to by p
.