 Hey everybody, this is Brian. Welcome to the, what are we on, the 10th Flutter tutorial. We're going to talk about abstraction today. So what is an abstract class? Use the abstract modifier to define an abstract class, a class that can't be instanated, meaning you can't make an instance of it. Abstract classes are useful for defining interfaces. What is that? What are we talking about here? So there's a few key concepts we have to understand. First, an abstract class cannot be created. You can't use the new keyword. Second, we're going to be defining something called an interface. An interface, just think of that simply as a contract. So when we look at, this is the previous tutorial. We had the mammal class, which has the has backbone has hair, and we're setting the value actually in the mammal class. The problem with that is when we change something down here, it blew up the entire stack. So if we change it here, suddenly feline to stopped working lion stopped working, we're going to fix all that today. So let's just go new project. We're going to make a new dark console application. We're going to call this classes for, yes, let's do this window. And that will calculate the meaning of life and everything else. All right, so let's just get that out of there. Somebody emailed me and said, Hey, stop complaining about how slow that is. Mine's really slow. So we're going to make a new you guessed it mammal file. And we're going to say abstract class. And we're going to say bull has backbone. I've got weekend on the brain. It's my it's technically my Friday. It's the day before Thanksgiving. I'm just kind of cranking out a couple tutorials here and avoid speak. So what is going on here? This looks like a normal class, but there's a few minor changes. The abstract puts the abstraction to this class. And then we have some elements here that don't have a value. That's actually part of our interface. What we're saying is anything that inherits this class has to include these. Think of an interface as a contract. It's a lot like buying a car. So let's go out and say feline office chairs driving me nuts. Sorry about that. Mammal. And we're going to say abstract class feline. Oops. Extends mammal. So far, nothing's really changed here, right? Bull has claws. Let's play around with this and see what happens. Let's actually go out to our main and we're going to import that feline class here. I'm going to say feline cat equal new feline. And oh, we already have a problem. As you have suspected, abstract classes cannot be created with the new expression. We can't instantiate them. Can't make a new instance of it. So how do we actually do that? Well, we have to make a concrete class here. So let's actually go file new. We're going to make a lion class here because why not? Whoops, lost my place in my notes here. All right. So in our lion class, we're going to actually import the feline. And then we're going to say class lion extends feline. Oops, that is not what I wanted. There we go. Now it's giving us some sort of other issue. It's saying, okay, missing concrete implementations because this is a concrete class, not an abstract class. We have to actually now implement that interface or actually interfaces because this is an inherited interface. So when we mouse over here, you can see that it has claws, has backbone, has hair. So it's missing the implementations from feline and mammal. And you can actually right click on this. Go to generate and then implement methods. And you can see exactly what it's missing and what it's from. You can even double click and create it, which I'm not a big fan of because I don't like the way it does it. But if you're curious, it just looks like that. So we're going to say bull has backbone. And we're going to actually implement this now. Bull has hair. Bull has claws. The spell has twice. Wow. So we have everything that we need now. We can actually make an instance of this thing. So let's change this. We can now actually make an instance of this thing. And we can actually dot speak and see what we want it to do. Now I've never heard a lion say hello world. So we're going to change that a little bit here. We're going to go in here and we're going to say we're going to make the lion roar instead. So you can see how it's very similar to actually just inheriting and extending a class. But there's some key caveats here. First off, you're not setting this value in the abstract class. You're not setting the actual getter. So that's called an interface. You actually have to have that in your concrete class. Without it, it will actually generate an error. See how suddenly it's complaining, missing concrete implementation of mammal dot has backbone. You would do that for certain things. For example, if you're building a car or truck, not all cars have all the same features, but you want certain things that make up a car, car has four wheels. For example, car has a windshield most of the time. If you pay extra, usually it has a windshield. So the other key component here that we're missing is you can actually do some really cool things with abstraction interfaces. So we're going to actually make another file here and call it a bird. And we're going to say abstract class bird. And we're going to say bull has wings. Now let's just get a little crazy here. Let's make another one. Scorpion. I don't know what that thing is called on the back of a scorpion. I just call it a stinger. Why not? So what are we doing here? We're just making abstract classes. We're actually making interfaces. So we're going to make a concrete class now and we're going to call it monster. We're going to actually build a monster here because that's just how cool we are. We have nothing better to do. We're going to build a monster. So we're going to make our bird or we're going to import a bird. We're going to import the scorpion and we're going to actually import the feline. And this is an example of interfaces. So I'm just going to put that in case anybody's wondering. So we're going to say class monster implements. And we're going to say bird feline and scorpion. So now we've got multiple inheritance going on here. But as you can tell, we also have multiple things that we're going to have to make concrete implementations of. So we're going to say bull has stinger. Yeah, let's give our monster stingers. And let's say bull has hair. Do we want a hairy monster? Why not? And through the magical copy and paste, we're just going to couple these. So we're going to do that. And then we're going to say I don't know what noise a monster would make. I mean, some gobbledygook. I don't know. That's a monster noise, ladies and gentlemen. That's what that is. All right. So what are we missing here? Has claws and has hair. Yep, because we misspelled this. All right. So now we've got our monster class. You can see this little icon over here and you can actually see implements method has claws and feline. You can actually click on that and it will take you right to the implementation of it. All right. So let's go back to our monster here and let's go to this chair. Man, my whole little office here is just falling apart. All right. So monster. What do we want to call our muscles called the Babadook? If you've ever seen that movie, it's kind of a crazy movie. And we make the Babadook speak. And why not? Let's just run this. See what happens. Bang. So there's our lion and there's our monster making some crazy noise. Lot of work. Great. But what has it really bought us? Well, we now have two different extensions of this feline class in lion and in monster. And you can see how we have multiple inheritance going on here. And notice the use of the implements keyword here. That is key. If you try to do extends, that's a different thing. It's kind of hard to really explain. But when you're extending a class, you're extending the class. You're taking it and making it more. When you're implementing it, you're actually implementing it. You're saying, I'm going to follow the directions. So really, we're just using drum roll drum roll. You guessed it interfaces through abstraction. And what has it bought us? Well, we can actually say we don't want our monster to have hair. And when we run, there's no error. Where if we had done this through pure inheritance and we put has hair all the way back up in mammal, because we inherit feline, this whole thing would have just exploded. And it would have said, nope, assuming we have done an assert down here. That's a lot of talking. But as you can see, you can do some really just crazy outlandish things. You can make like a airplane that's also a refrigerator or you can make a car that's also a helicopter. So that's a really good example of inheritance class abstraction and interfaces. For the source code for this and other tutorials, I encourage you to visit my website void realms.com, click on tutorials and strum roll. There is a void realms Facebook group with 1700 other programmers in it. Go ahead and click that link. It takes you right to it and ask to join.