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 C# Objects!
You have completed C# Objects!
Preview
We can create new types that inherit the attributes and behaviors of existing types.
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
[MUSIC]
0:00
In the first part of this course we
wrote a class that models a point.
0:04
It has x and y fields and
0:08
even has a method that calculates
how far it is from another point.
0:11
The Treehouse defense game uses this
class to identify locations on the map.
0:16
But it could be used for
a lot more than that.
0:21
Lots of software applications use x and
y coordinates to identify a location.
0:24
Perhaps the next game we write
will also need a point class.
0:29
Software that displays charts and
graphs also needs points.
0:32
If we keep our implementation of the point
class as general purpose as possible,
0:36
we can reuse it in all those
different applications.
0:41
What if we need to add something
to the point class that's specific
0:45
to Treehouse defense?
0:48
For example what if we wanted to make
it impossible to create a point that
0:50
isn't on the map?
0:54
Making a change like that to the point
class would make it unusable to
0:55
all other applications
that don't have maps.
0:59
We don't want to add code to
a general purpose class that we're
1:02
fairly certain won't be useful
to other users of the class.
1:05
We also don't want to create a new class
that's specific to Treehouse defense and
1:09
rewrite everything we wrote for
the point class.
1:14
What we want is to create a new class
that has our new functionality, but
1:17
also reuses the point class.
1:21
The principles of
object-oriented programming
1:24
provide lots of ways to reuse and
extend existing classes.
1:26
One of them is called inheritance.
1:31
There are four core principles
of object-oriented programming.
1:34
Encapsulation, inheritance,
polymorphism and abstraction.
1:38
By saying that C# is an object-oriented
programming language, we're really saying
1:43
that C# has features built into the
language to support these four principles.
1:48
As you can see, inheritance is
one of the four core principles.
1:54
Don't worry about these other
three principles right now.
1:59
We'll discuss them later.
2:02
The principle we want to focus
on right now is inheritance.
2:04
You see,
2:08
many objects in the real world often share
many of the same attributes and behaviors.
2:09
A classic example is an animal,
all animals have similar characteristics.
2:15
For example, a defining characteristic
of all animals is the ability to move.
2:20
There are two types of animals,
vertebrates and invertebrates.
2:25
Vertebrates have backbones and
invertebrates don't.
2:30
But they're both still animals,
so they can both move.
2:34
Mammals and
birds are types of vertebrates.
2:37
By saying that a creature
is a mammal we're
2:40
also saying that it has a backbone and
it can move.
2:43
You might say,
2:46
that it inherits these characteristics
from the larger categories,
2:47
or classifications, of vertebrates and
animals of which it's a member.
2:51
You can see now that the C# key word
class is short for classification.
2:56
C# classes define what it means to
be in a particular classification.
3:02
As we saw with the animal example,
classes of animals
3:07
can have more refined classifications
within them, we call these subclasses.
3:11
Subclasses inherit the attributes and
behaviors of the more general classes.
3:16
So if we wanted a new type
of point that inherited
3:22
all of the features of the point class,
but
3:25
was more specific to our application, we
just need to create a subclass of point.
3:28
Let's look at how we do that in code.
3:33
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