Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Intermediate C#!
You have completed Intermediate C#!
Preview
Property accessors can also be protected.
Correction
In the video, the health value of StrongInvader
was not changed to 3
as intended. It is left as 2
. Be sure to update this value if you are following along using Workspaces.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's create one more new
type of invader together.
0:00
The invader class has another property
that we haven't made overridable yet,
0:04
the health property.
0:08
Let's do that now by adding
the virtual keyword here.
0:10
Now, we can have different
types of invaders
0:14
that have different
levels of initial health.
0:17
Let's create a StrongInvader
class in StrongInvader.cs.
0:19
To save on typing, I'm going to copy and
paste FastInvader and make changes to it.
0:25
We'll replace fast with strong.
0:32
And delete the StepSize property.
0:40
Let's copy the health
property from invader.
0:43
And we'll need to change
this virtual to override.
0:51
Now that we have a StrongInvader,
let's add it to the level.
0:57
The invaders in this level are shaping
up to be quite formidable.
1:00
We'd better make some enhanced
towers to combat them.
1:04
Let's make sure everything
compiles first though.
1:07
Oh-oh, we got a compiler error.
1:09
It says,
TreehouseDefense.StrongInvader.Health
1:13
is marked as an override, but no
accessible set accessor found to override.
1:19
I think I know what happened.
1:27
You see, the setter of
the health property is private.
1:28
This means we can't access
it from strong invader.
1:33
Getters and setters of properties can
have their own access modifiers, so
1:36
long as they are more restrictive than
the accessibility of the property they
1:41
belong to.
1:44
In invader.cs, we can change
private here to protected and
1:46
this will give StrongInvader
access to health property setter.
1:50
Health is a public property.
1:55
So, by default, its getter and
1:58
setter are both public unless
we change their accessibility.
2:00
If health was a protected property, we
wouldn't need to have the protected access
2:04
modifier here, because both the getter and
the setter would already be protected.
2:08
We want health to be public,
but we still don't want classes
2:14
other than sub classes to be able
to change the health directly.
2:19
So we want the setter to be protected.
2:22
Let's compile again to
see if this fix things.
2:25
They have another compiler error.
2:31
TreehouseDefense.StrongInvader.Health.set
cannot
2:33
change access modifier when
overriding protected inherited
2:38
member
TreehouseDefense.Invader.Health.set.
2:43
I think I know what happened here, too.
2:48
I changed the setter to
protected in invader, but
2:51
I didn't change it in StrongInvader.
2:54
The access modifiers of
overridden properties
2:57
must match the property of the override,
so this setter must also be protected.
3:00
Let's compile again to
see if this fix things.
3:05
All good.
3:08
All is well.
3:09
We just learned that sub classes can't
change the interface to the properties and
3:10
methods they override.
3:14
If we further sub classed StrongInvader,
3:16
the accessibility of the members of
that class would have to match these.
3:19
Meaning, the health property would need
to be public and its setter, protected.
3:24
We can't change it in sub classes.
3:30
We've got lots of types of invaders now,
let's make some more types of towers, too.
3:32
We can make towers that have
different ranges, power and accuracy.
3:37
Right now range, power and
accuracy are private constant fields.
3:42
So, we want to change these two
protected virtual properties so
3:48
they can be overridden in sub classes.
3:52
Let's go ahead and do that right now.
3:55
So change this to protected and
3:57
virtual, and
get rid of the const modifier.
4:01
And because it's a property and
it's protected, the common convention
4:08
is to start protected member
names with an uppercase letter.
4:12
And we'll make it a read only property
by just giving it a getter, and
4:17
it's initialized to one.
4:22
Let's do this with the rest of the fields.
4:24
We changed the names of these members.
4:43
So, we'll also need to fix the names
where these properties are used.
4:45
So we'll change this underscore our range
to capital our range, underscore A,
4:50
accuracy to capital A, Accuracy, and
underscore P, power, to capital P, Power.
4:55
Just make sure that we've got everything.
5:03
All right, that looks good.
5:06
Now, we can create new types of towers.
5:07
We can make new types of towers that
have more power, longer ranges,
5:10
or different accuracies.
5:14
But by now,
you already know how to do this.
5:16
Why don't you go ahead and
5:20
create some sub classes of tower on
your own that override these properties.
5:21
I suggest making a powerful
tower with more power,
5:26
a sniper tower with higher accuracy, and
a long range tower with a longer range.
5:29
You can even make a super tower
with more than one enhancement.
5:35
I won't walk through creating
these new tower types here,
5:39
because you know everything you
need to know to make them yourself.
5:42
I've included the code for
sniper tower, powerful tower, and
5:45
long-range tower in the download for
this video.
5:49
I encourage you to try to create at least
one new type of tower before moving on.
5:52
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up