-
Create a class called
FISPet
.FISPet
has a method calledmakeASound
that returns anNSString
that reads "Pet me!" It also has a method calledeatSomething
that returns anNSString
that reads "Nom nom nom." -
Create a second class called
FISDog
.FISDog
should be a subclass ofFISPet
. TheFISDog
class should overridemakeASound
to return "Woof!" Also,FISDog
s are able toassaultTheMailman
. This method returns anNSString
that reads "I got the mail! I got the mail!" -
In your FISAppDelegate
application:didFinishLaunchingWithOptions:
, create an instance ofFISDog
. Then...- Confirm that it barks and can assault the mailman!
- Cast it to an
FISPet
and confirm that, even though the variable is now anFISPet
, itsmakeASound
still goes "Woof." - Create an instance of
FISPet
. Cast it to be anFISDog
, and make it go "Pet me!" Can you callassaultTheMailman
on this fake dog? Will it compile? Will it run? - Now try to make your
FISDog
that was cast as anFISPet
assault the mailman. What happens? How might you make the method call work? See the hint below for more information.
It is possible to cast in-line by putting parentheses around an object. For instance, assume we had a FISCar
class and a FISPorsche
subclass, and the FISPorsche
object had a property called isTurboCharged
. The following would work:
FISCar *newCar = [[FISPorsche alloc] init];
((FISPorsche *)newCar).isTurboCharged = YES;
So, what is happening above? First, we have intialized a FISPorsche
, but it has been assigned to a variable of type FISCar
. Then when it is time to set the isTurboCharged property, we have re-cast newCar
to its original FISPorsche
type, so the compiler will let us access its isTurboCharged
property. Note the extra set of parentheses before .isTurboCharged
.
View PetCast on Learn.co and start learning to code for free.