Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Specialized car classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's define some other types.
0:02 We saw that we can no longer use car directly
0:04 'cause it's an abstract idea.
0:07 It's the base class we're going to use for our program
0:09 but we want to have specializations
0:11 that really implement the details.
0:13 So let's create a new file here called basic_car
0:16 something like that.
0:17 Bring another class called BasicCar like so.
0:21 Now it needs to derive from Car
0:23 so we want to write just Car here
0:25 and PyCharm says, well
0:26 looks like you don't have a definition of Car
0:28 on one of these two lines, so I have no idea what this is.
0:31 But we can import it up at the top.
0:34 We say from, models.car import Car.
0:37 And then down here, we can put nothing for just a second
0:40 and PyCharm says we need to implement
0:43 some of these abstract methods like refuel car. Cool.
0:47 So it's just going to go here and write this function
0:49 which right now is empty.
0:51 Not super interesting is it?
0:53 But we can print out something like this.
0:57 Put the type name here
0:58 so it's really clear where that detail's coming from.
1:01 The basic cars take any old gas.
1:05 Right, that's good.
1:08 Now here, let's see, this is going to be a BasicCar.
1:12 And again, to use that, up at the top
1:14 we don't have the right import statement.
1:15 PyCharm will add it.
1:17 Or you can write it yourself as we go up a little bit
1:19 there you go.
1:21 That let's us do this part here
1:23 and if we only do that one car, it works.
1:27 Look, the car part deals with the drive
1:30 and the Windstar goes vroom.
1:31 The basic car deals with refueling
1:34 and it says it will take any old gas.
1:36 Super. So that gives us our basic car.
1:38 Let's do a couple real quick here.
1:46 We're going to define another class, sports car
1:50 derives from car, just like before
1:52 we add the import statement above.
1:54 Now it's going to pay some print statements.
1:56 So we have the sports car does the such and such
1:59 tears along the highway, and it only wants the best gas.
2:02 That's true. Let's also add an electric car.
2:14 Once again, import the base class
2:16 implement the two functions, in this case.
2:19 This, what can I say, the electric car zooms along silently
2:23 as it has to change the behavior
2:25 and the refuel one, we have to implement
2:27 and it's charging up.
2:29 One thing to notice about the IDE here
2:31 check this out on the left.
2:32 Click on this, it takes us back
2:35 to where it's being implemented, where it comes from.
2:37 So drive, we see, did it say?
2:41 Overrides this method in car. Same here.
2:43 But if you have a rich hierarchy like Car, BasicCar
2:46 ElectricCar, ElectricSportsCar, whatever right
2:49 and you're overriding at different levels
2:50 it will tell you, then also when we're here
2:53 have the reverse. You can see who is implementing this
2:56 can see BasicCar, SportsCar, and ElectricCar
2:58 all implement refuel
2:59 and you can jump back and forth between them.
3:01 That's pretty slick.
3:03 Right, let's change the thing for creating here.
3:05 This will be a SportsCar and import that.
3:12 Let's make that SportsCar there like so.
3:16 This will be an ElectricCar.
3:22 You can just type E-C and do the CamelCase matching here
3:25 and get the ElectricCar to come up right away.
3:27 And up at the top, now we have the 3
3:29 that we're actually working with.
3:32 And now I just remove that in that car there
3:33 but let's change this real quick
3:35 and say that this is going to return a list of car.
3:40 And that comes from typing like so.
3:42 So this, when we come over here
3:44 we're going to say cars[0].
3:47 and we're certain to get the right things.
3:48 So we're still using our base type here
3:51 thing, this is the commonality you can assume
3:53 across all the collection that comes back
3:56 but of course, their behaviors are different
3:57 when we ask them to drive and refuel.
4:00 Whew, let's try it one more time
4:01 and see how it's going.
4:03 Cool, so now the sports car, the Corvette
4:06 tears along the highway, and it only wants the best gas.
4:08 The Windstar didn't override how it drives.
4:11 It just goes vroom.
4:12 But it had to say how it was fueled
4:14 and it takes any old gas.
4:16 Electric car zooms silently along
4:18 and it's charging up, the Tesla and the Volt.
4:21 Pretty cool, huh?
4:23 This is really nice, clean, full-featured OOP
4:27 and we've got more to come.
4:28 This is just the start of it.
4:29 But it's a pretty cool way to get started.