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


Talk Python's Mastodon Michael Kennedy's Mastodon