Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video

00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Abstract members of an abstract class don't have implementations. Subclasses can provide them though.
This video doesn't have any notes.
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
The abstract invader class has two virtual
properties and one virtual method.
0:00
Let's take a look at the four invader
subclasses we've created so far.
0:05
Here in fast invader, we've overridden
the step size property, but
0:10
not the health property, or
the decrease health method.
0:14
This is still using the default
implementations provided by
0:17
the invader class.
0:20
Here in strong invader,
we've overridden the health property but
0:22
not the step size or
decrease health method.
0:25
Here in shielded invader, we've only
overridden the decrease health method, and
0:28
here in basic invader,
none of the virtual properties or
0:33
methods are being overridden.
0:36
Basic invader just does what
the invader base class does.
0:38
But what if we wanted to be sure that when
developers create new types of invaders
0:41
that they purposefully state what their
invaders initial health should be.
0:45
This way we can be sure that they
haven't accidentally use the default
0:49
implementation.
0:53
After all who are we to say what the
default health of the invaders should be?
0:54
Often times, we don't know what the
default implementation of a property or
0:58
method should be when writing
an abstract base class.
1:02
This really should be left up to
the concrete implementation of
1:05
each type of invader.
1:08
We can do this by making
the health property abstract
1:10
here in the invader class we'll change
the keyword virtual here to abstract.
1:14
By making it abstract we're
saying that the health property
1:19
must be overridden in subclasses.
1:22
Abstract properties can't
have implementation so
1:25
we need to delete where
initializing it here.
1:28
An abstract member is
the same as a virtual member
1:31
except that it doesn't have
its own implementation.
1:34
In the abstract base class, we can state
if it should have a getter or setter and
1:37
what access level the getter and
setter should be.
1:41
But we can't say what the getter and
setter should do when called.
1:43
We have to provide the actual
implementation in the subclasses.
1:47
If we compile our program we'll get some
compilation errors saying that basic
1:51
invader, fast invader and shielded invader
don't implement the health property.
1:55
We'll need to fix this.
2:00
We can fix this by providing
an implementation of the health property
2:02
in each of these subclasses
that doesn't have one.
2:05
So we'll copy the property from
the invader base class and
2:08
then fast invader, we'll paste it,
change abstract to override.
2:12
And then initialize it to two.
2:20
We'll copy this implementation to each
of the other classes that need it.
2:23
On surface it appears that
our code is no longer dry.
2:35
DRY is a basic principle
of computer programming.
2:40
If you remember, DRY stands for
Don't Repeat Yourself.
2:42
Copying and pasting code like this
should always cause some red flags and
2:46
make us stop to think about
what we're doing and why.
2:50
If we're copying and pasting code like
this, perhaps this code should be in
2:52
the base class where it can
be shared by subclasses.
2:56
On the other hand if we don't want to
have a shared default implementation.
2:58
The only option is to have each subclass
provide their own implementation.
3:02
Some classes may want to have
the same implementation,
3:07
so it may appear that
this is duplicate code.
3:10
There are many things to think about when
architecting an object oriented software
3:13
application.
3:17
We've just seen an example
of an abstract property.
3:18
Methods can also be abstract.
3:21
We can make them abstract the same
way we make properties abstract.
3:23
First we add the abstract keyword and
then remove the implementation.
3:26
In the case of a method we just put a
semi-colon after the closing parentheses.
3:30
This is how we do that
with the move method.
3:35
In our case we like the default
implementation of the move method and
3:37
I don't see any reason to force it
to be implemented in subclasses so
3:41
we'll keep it as it is.
3:45
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