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