WEBVTT

1
00:00:00.440 --> 00:00:01.900
Polymorphism and

2
00:00:01.900 --> 00:00:06.780
virtual overridable methods can be
difficult to understand at first.

3
00:00:06.780 --> 00:00:09.010
So let's work through another example.

4
00:00:09.010 --> 00:00:09.930
This time,

5
00:00:09.930 --> 00:00:14.240
we'll make a couple classes using
the animal example we discussed earlier.

6
00:00:14.240 --> 00:00:16.230
We'll do this in the C# repel.

7
00:00:16.230 --> 00:00:19.510
To start the repel,
just type csharp in the console.

8
00:00:19.510 --> 00:00:22.880
Let's have a Bird class that
contains a Move method.

9
00:00:22.880 --> 00:00:26.040
This method will simply print Birds fly.

10
00:00:26.040 --> 00:00:29.628
We'll make it virtual so
that we can overwrite it in subclasses.

11
00:00:45.529 --> 00:00:48.629
We'll make a subclass of
the bird class named Penguin.

12
00:00:53.452 --> 00:00:57.890
In the Penguins Move method,
we'll print, Penguins waddle.

13
00:00:57.890 --> 00:01:01.013
We need to remember to type override
here cuz this is the subclass.

14
00:01:15.088 --> 00:01:18.506
Now let's instantiate a Bird and
call its Move method.

15
00:01:23.343 --> 00:01:26.166
As expected, it says Birds fly.

16
00:01:26.166 --> 00:01:32.224
Let's instantiate a Penguin and
call its Move method.

17
00:01:37.935 --> 00:01:41.852
Again, as expected,
it prints Penguins waddle.

18
00:01:41.852 --> 00:01:46.143
Now let's instantiate a penguin but
assign it to a bird variable.

19
00:01:49.975 --> 00:01:54.175
Remember we can do this
because penguins are birds.

20
00:01:54.175 --> 00:01:56.720
Even though the variable is of type bird,

21
00:01:56.720 --> 00:02:00.170
the object it stores is
really of type penguin.

22
00:02:00.170 --> 00:02:02.792
Now let's see what happens when
we call Move on this variable.

23
00:02:05.785 --> 00:02:08.380
It prints Penguins waddle.

24
00:02:08.380 --> 00:02:11.740
Even though we're calling
Move on a bird variable,

25
00:02:11.740 --> 00:02:15.410
the Move method in the bird
class isn't being executed.

26
00:02:15.410 --> 00:02:19.990
Instead, it's the Move method in
the penguin class that was executed.

27
00:02:19.990 --> 00:02:23.430
The Move method in the bird class
was overridden by the Move method in

28
00:02:23.430 --> 00:02:25.080
the penguin class.

29
00:02:25.080 --> 00:02:28.120
This is a very powerful
feature of C# because

30
00:02:28.120 --> 00:02:31.820
we can write code that only
deals with bird variables.

31
00:02:31.820 --> 00:02:34.340
Later, as we add new types of birds,

32
00:02:34.340 --> 00:02:38.630
the code still works as expected
without making any changes.

33
00:02:38.630 --> 00:02:43.240
This is why we were able to add a new type
of invader without changing the logic

34
00:02:43.240 --> 00:02:47.690
of the game as it's written in
the play and fire on invader methods.

35
00:02:47.690 --> 00:02:50.620
The changes are contained
inside ShieldedInvader and

36
00:02:50.620 --> 00:02:54.020
the rest of the game treats it
like any other type of invader.

37
00:02:54.020 --> 00:02:56.910
We'll get lots of practice
using polymorphism to

38
00:02:56.910 --> 00:02:58.980
create new types in our game.

39
00:02:58.980 --> 00:03:02.380
Keep note as we go along that
the core logic of the game,

40
00:03:02.380 --> 00:03:05.770
as it's coded in the play method,
never changes.

41
00:03:05.770 --> 00:03:08.810
This is the power of polymorphism and
virtual methods.
