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
Let's continue our intro to object-orientation with a discussion of what classes and objects are and what they can do. Touching on concepts like instances, inheritance, properties and composition, we'll lay the theoretical foundation for much of this course.
-
0:00
Let's talk about classes.
-
0:01
To do that, let's imagine we own a factory, a factory that builds cars.
-
0:07
So here we are standing outside our factory.
-
0:10
We can't really see what's inside, so let's peek in.
-
0:13
Okay, much as I expected, we've got a big long assembly line and
-
0:17
a bunch of machines.
-
0:18
We've got a long list of specifications that describe what a car is,
-
0:21
what it does, and how you build it.
-
0:23
Let's take a look at that.
-
0:25
Four wheels, one steering wheel, five seat belts, one gas engine,
-
0:29
exhaust, bumping stereo, paint color, windows,
-
0:32
four doors, car can drive, can turn, can honk, and stop.
-
0:38
That all make sense, it's the parts and functionalities of a car, great.
-
0:42
So what?
-
0:43
Well, this factory including the specifications of what a car is and
-
0:46
the instructions and machines to put one together,
-
0:49
that's a class, specifically it's the class car.
-
0:53
It's pretty noisy in here and confusing with all these machines.
-
0:57
Let's go back outside.
-
0:59
Much better.
-
1:00
Wait.
-
1:01
Something is about to happen.
-
1:03
You see that thing that just popped out?
-
1:05
That's an object specifically it's an instance of the class car.
-
1:10
Wait, it's happening again, another object.
-
1:13
That object, too, is an instance of the class car.
-
1:17
Now remember that list of specifications for what a car is made of?
-
1:20
Those are just variants, or properties, that we can manipulate.
-
1:23
I want a blue car.
-
1:26
Now I want to red car.
-
1:28
Blue car, red car, drive.
-
1:31
Blue car, your wheels equals zero.
-
1:34
See where we're going with this?
-
1:35
We built the factory, that is to say the class, and now we can just pump out cars.
-
1:40
We also know just how each of the instances, a.k.a the cars themselves,
-
1:44
are built and we can very easily change them, like we just took off the wheels.
-
1:50
>> Blue car, you have four wheels, blue car, red car drive.
-
1:55
I want a silver car.
-
1:56
>> Silver car drive.
-
1:58
>> Did you notice that we can't see inside the factory?
-
2:01
In fact my new friend here has never even been in the factory, but
-
2:04
he can still bark orders at it and the cars and they both know how to react.
-
2:08
That is called in Encapsulation.
-
2:10
It means that external people, or objects really,
-
2:13
can manipulate other objects with minimal knowledge of their internal workings.
-
2:17
Think about how useful that would be when you have a team of developers
-
2:20
all working on a video game.
-
2:21
The guy building the levels of the game doesn't have to worry that his
-
2:24
changes will break the characters who were designed by someone else.
-
2:28
Encapsulation is a driving principle so to speak of object orientation.
-
2:33
>> I want a two seater convertible.
-
2:34
[NOISE] >> That won't work,
-
2:37
our factory doesn't know what that means but hold on a sec.
-
2:41
Build me another factory, make it just like the last one,
-
2:45
except create a new property called roof.
-
2:47
Set that to 0.
-
2:48
Add a property for the number of seats, set that to 2.
-
2:52
And yeah, your property for doors?
-
2:54
Assign that the value 2 also.
-
2:56
Now make me a blue two seater convertible.
-
3:00
Blue convertible drive.
-
3:02
Blue convertible stop.
-
3:05
You see how that convertible already knew how to drive and stop?
-
3:09
That's because the convertible class is a subclass of car.
-
3:12
So it inherits all of car's properties.
-
3:14
Convertibles will be exactly the same as cars,
-
3:17
except where we explicitly specify otherwise.
-
3:20
When you create a subclass from a superclass,
-
3:23
you inherit the superclass' properties by default.
-
3:26
Inheritance, like encapsulation,
-
3:28
is one of the critical features of object orientation.
-
3:32
>> I want a school bus factory >> That won't work, but
-
3:36
I've got an idea >> Create a subclass?
-
3:40
>> No, school buses are just too different from our cars.
-
3:43
But you know the engine, windows, paint, and steering wheel?
-
3:46
Those parts are the same, and those little parts are objects, too.
-
3:50
Build me a new factory.
-
3:51
Let's use the engine, window, paint and steering wheel objects to start with.
-
3:56
Now make it equipped to build a vehicle 10 meters long with 15 rows of big seats,
-
4:01
a funny door in the front and back, and of course paint that sucker yellow.
-
4:06
Now build me a school bus.
-
4:08
So we decided that a school bus is just too different from a car
-
4:12
to use car as a super class.
-
4:14
However, we did notice that some of the objects which make up a car
-
4:18
could be repurposed to compose our new class called school bus.
-
4:22
Doing that is called Composition.
-
4:24
Composition is an alternative to inheritance and
-
4:27
also a critical feature of object orientation.
-
4:31
In our next few videos, we'll start working with some classes and objects and
-
4:34
hopefully that will begin to solidify things.
-
4:36
We'll also be coming back to our car factory throughout the rest of the course
-
4:40
to help reinforce the concepts.
-
4:41
But again, don't stress about it.
-
4:43
These are big concepts and it'll take some time, and
-
4:46
perhaps some different perspectives, to really bring it home.
-
4:49
Hop into the short quiz we've prepared for
-
4:51
you and then move along to some more concrete examples of objects and classes.
You need to sign up for Treehouse in order to download course files.
Sign up