 Hey everybody, this is Brian and welcome to the seventh Dart tutorial. Alright, so today we're going to click language and then go back to tour. And what we're going to start today is called classes. You may be sitting in a class watching this video, but this is different. So first off, what is a class? If you've never come from any other language, it'll say dart is an object oriented language with classes and mixing based inheritance. What in the heck does that mean? What it really means is a class is a blueprint. From that blueprint, that blueprint, you create an instance of that object. So let's say you have a house. Well, house is a abstract concept, right? There's different types of houses that come in all sizes, shapes, colors, rooms, locations. So a house would be your blueprint. From that, you would instigate or make instances of houses. So you would actually build the houses from those blueprints. And that's kind of what we're going to be doing today. Alright, so we're going to create a new project here. And we're going to make sure it's console application and Dart. And let's call this classes one, even though it's really like intro to classes. So while that is just grinding away, calculating the trajectory of everything in the universe, what does the mixing based inheritance mean? You'll see this passed around quite a bit. Really, what that means is you can have multiple classes and have those classes inherit from each other. For example, you are inherited from your mother and your father. So let's say your mother has red hair and your father has blue eyes, you probably have red hair and blue eyes. Actually, I think most red heads have blue eyes anyways. When I had hair, I was a redhead, but anyways. So let's just dive right in here. So first thing I want to cover is why do we need classes to begin with? We can already put functions in here. So let's just say we want to do void print speak. Whoops. That was just stupid. We want to speak. And then in here, we're going to print something, right? And we can call this pretty much from wherever we want, right? We've done this before. And we're going to say, so when we run this, we know absolutely what to expect. It's speaking. But let's say we want to mimic a dog or a cat. Then we've got to change this. So let's say string, I don't know what to call this noise. And then we're going to add the noise here. And so we forgot to add this. So now we want to say we're going to just imitate a cat here. Yeah, why not? And now it's going to be out. But what if we want to do a dog now? Well, okay, I guess we could just do this and then bark instead. But we are now imitating a dog and a cat, but we don't actually have a dog and a cat. What if we want to add things like fur color, eye color, you know, name, things like that? That's where classes really shine. So first thing we're going to do is make a new Dart file. Let's just call it cat.dart. And notice how I put it in the bin file. If you read the official documentation, the lib is actually really for external or public things. So if you want to share that with the rest of the world, where bin is really for your internal program, I may be wrong on that. But that's what I read. And whoops, we don't say void. We say class because we're giving an actual type. Now this is not a return type. This is a declaration. Notice how it doesn't take any parentheses. It just has these brackets. So there's no parameters here. We will get into that. They're called constructors. We'll get into that into a future tutorial. But right now we're going to keep it simple here. So now we're going to say void, speak, print, meow. And we're going to just through the magic copy and paste, make a new file, call this dog, go back to our main file here. Let's just get rid of these. First thing we really need to do is actually import those. So we're going to import. And we're going to import cat.dart. And we're going to import dog.dart. Thought my phone made a weird noise. I just heard that. Anyways, I dropped my phone and it like the camera case cracked. The phone itself is fine, but I had to order a special part of it. Now the phone's acting just weird. All right. So now that we have our classes, our blueprints, we have to actually make instances of those objects. So we're going to say cat. And I don't know what to call the cat. Let's call him Mr. Twinkles. I have no idea what to name the cat equal new cat. So what are we doing here? We've seen something similar. So we're declaring a type, which is actually our class, our blueprint, this guy right here. And then we're saying our variable name. And then we're going to actually assign it a value and we're saying new new is a keyword new cat. Meaning we're going to take that cat blueprint, make a brand new version of that and hand it to this variable. We could also do this. So now we have two instances of the same blueprint, but they're two different objects. We have an object here and an object here. And we can say now we have an instance of our dog class. And then we can say Mr. Twinkles speak fluffy speak. And we'll say what do we call him Fido speak. I prefer to do lowercase, but we should because their names I'm kind of weird about that sometimes. So when we run this bad boy, you'll see we have meow meow barks. We've got both cats meowing and dog barking here. So we got a noisy room. Okay, great. But what are we really leveraging here? What's the real power of classes? What we can do here is say like bull claws out. And with the dog, we can say string color. So what we now now is our blueprints are different, right? Both of them have a speak, but dog has a color cat has a clause out. And we can access those through the properties. So let's say Fido, oops, not final Fido. There we go. And let's just print this bad boy. My spelling skills are much to be desired today. There we go. So you can see we now have brown after bark because we're printing out Fido's color. So that is in a nutshell how you would build a class and how you would actually make your blueprint. Now there's a lot that goes into this. We're going to split this up over several videos. And what I want to do now is actually make a bit more of a complex example. And to do that, we're going to kind of abandon the dog cat example, we're going to make a new file and we're going to call it, let's call it house. And in here, we're going to say class house. And we're going to make our scope. Now, we're going to make a private variable here. We're going to say h. And then we're going to make a public variable. And then we're just going to make a couple properties that are public here. We're going to say int width int height. Ah, that was my phone. I did hear it. Go away phone. I'm doing a tutorial. And then what is the difference between private and public? Some of you know other languages understand this. But some of you that are new may not. Notice how it has this underscore in the beginning of it. That is how you differentiate between private and public. Private means that only this class inside of the scope can access this variable. Public means anything in the class and outside of it can access it. So good example. Let's just import here. I was gonna say house one equal new house. So if we try to say house one dot, and we do that underscore age equal 99 years old, you notice how it is not liking that. It's going to say the setter age is undefined in class house. What does that mean the setter age? That makes literally no sense. What's going on here? So let's try house one dot width equal, we'll say this is 100 feet and house one dot height. Actually, we should have called that length, but actually let's change that because I want this to actually make sense. And let's say this is 200 feet long. That's kind of a big house. Those take it just fine. So what is this complaining about? Let's just try to run it. And of course, boom, same error. Class house has no instance setter underscore age. It's doing that because we have a scope issue. This is a private variable and we're trying to access it outside of the class. Now if we're in the class itself, and let's just say notice how it suddenly works, that is called scope within the class. You cannot access that outside of this class or the scope whenever you see these brackets, think of scope, because it is private private is denoted by that underscore that is specific to this language to definitely pay attention to that. In most other languages, you actually write the word public or private in front of the type. Dart goes away from that whole concept. But fair not, there is an easy way to set that. And we're going to show that right now. What we're going to do is a getter and setter. So we're going to say, you remember the error message that popped out with is there is no setter. What's a setter? Well, this is a concept that sprung up about, I want to say 20 some odd years ago. And we're just going to go over it real quick. So when we get something, you're taking something, right? You're saying give it to me. So we're going to get, and we're going to say footage. And then we're going to do fat arrow width times length. What have we done here? We're getting a getter. We're getting a getter. We're creating a getter using the get keyword that is an integer type. The variable's called footage. The fat error really basically says we're making an anonymous function here. Because it's all one line, we don't have to break this into like something like this int footage return with times length. Yeah, you don't have to do something like that. So we're just going to delete that because it's all one line, right? It's all compact. You can definitely do it the other way, but that's what all this fat error really does. It just condenses it to one line. So we're getting that. And we have this anonymous function here, or as an inline function, anonymous versus inline, that always messes me up. So let's get our square footage from this thing. Let's actually delete that. So to run. And we're going to say print house one footage. So when we run this, you can see this is a large house. Let's scale that down. That's like, you know, massive house. Let's just say this is a length of 50 feet, actually 25 feet by 60 foot house. Okay, that's more reasonable. That's about a normal house size here. So how is it calculating? How's it getting this footage? Well, back to our blueprint here, we got our getter. And the getter is saying, Hey, whenever you try to access this variable, run this code. And all that's doing is taking our variables with the length and multiplying them and then returning them. Pretty simple. But what if we want to actually set something? We want to set the age of the house up here. So let's do that. Now, we have another problem is we can't access that because it's a private variable. That's where getters and setters work in tandem. So we're going to say it get. And let's just call this home age. That way. Now let's call this years. That way there's no real real issue between underscore age and age. And we're going to say return age. And then we're going to set home. I'm sorry, years old value. I want to give it a type there. So what we're really doing is we're saying, Okay, when you try to get something out of this, when you're trying to access that return this, which is actually up here, this private variable. However, when you're trying to set it or save something to think of variable as a container, like a little bucket. Remember, a variable is something that will change the contents of that bucket will change. So when we go to pull something out of that bucket, we're calling get when we go to put something into the bucket, we're calling set. Think of it in terms of those concepts and it'll make much more sense. Now set and get you have to have a type here. So we want to get we want to pull out of that bucket and integer and set we want to put into that bucket and integer. And that's how we're actually doing that. So when we go to pull out of the bucket, this will get run when we go to put into the bucket, this will get run. That's a very confusing concept if you're new to programming. That's why I kind of really wanted to cover it. So we're going to say house one that years old equal say it's a 10 year old house. And then we're going to print house one that years old. Let's run this. And you can see there's our 10. So here, we're calling the setter here, we're calling the getter. Getters and setters are important because sometimes you don't want to actually expose your variable to the outside world or you want to control how it's accessed. For example, let's say you want to set this but yeah, you're running a real estate agency and older houses really are more attractive. So you want to add 10 years to that house. So what we're doing here is a little sneaky, right? Because we're saying house one years old is 10. But under the hood, in the background, we're actually adding 10 years. So it's now 20 years old. So always think of getters and setters as if you're working with a bucket and it'll be much easier in your head. And that's why you need to use them. So you can actually isolate that variable from the outside world. So that's great and all. What happens if we have multiple homes here? So let's just make a new house. Let's make a couple of them. And let's add a property in here. Oh, we've already got it. Color. And we're going to go back here and we're going to say I want a red house, house to that color. I want a blue house, then house three. I need a good color. I need like a solid color that's just going to stand up black. You don't see any black houses. I don't know why black is pretty cool color. It could be really trendy to have like a completely black house. I'd buy one. So back to our trusty list example with generic programming. Remember how we have worked with, I think strings and integers in the past. See how this is list and then the little arrows and then E. That is the template we're going to be working with. And we are going to say we want to use our house class. Notice how maybe it's not visible. It's got that little C right here. It wasn't too visible when I'm on it, but it's got that C that denotes it's a class. So we're going to use the class, not an actual variable. We're going to call this homes equal new list house. Now that statement that we've worked in the past makes a little more sense because we know now that list is actually under the hood a class. And this just means it's using generic programming, which we will use in future tutorials. Now we're just going to say homes dot for each. And in here, we're going to say, whoopsie, I always come on keyboard work with me here. There we go. We are just going to do our function. I want to say value. And we're going to say print value dot color. So really what we're doing here is we're saying make a list. And in that list, we're going to store homes or the house class that blueprint. And for each of them, we're going to print out the color. And you can see how it is now where to go. Why did it not do that? There's brown print value dot color. Did I screw that up? You know, it's been a very long day when you just completely forget to do something. There's nothing in our list. That's why it wasn't working. My brain is totally fried. I have bought the house. I am currently doing a lot of stuff at work. So bear with me if I'm a little scattered brain sometimes. Homeownership will definitely make you lose your mind. I spent all weekend installing these lights. It should have been the easiest thing in the world. After about a million cuss words, the lights are installed and working though. There we go. So there's our three houses with the three different colors. And in there, you can access the other properties and things too, like, you know, footage, things like that. Alright, so that is in a nutshell intro to classes. I hope you found this educational and entertaining. We're going to cover these a little more in-depth. Be sure to really read up on classes on the official Dart site. And for the source code, go out to my website, voidroms.com, click tutorials, and there's a link to my GitHub repos. I do have it out here as flutter tutorials. And then I have them as sub modules in here, and I'll put in the additional sub modules. If you're so inclined, if this helped you, definitely consider donating. This site's run 100% off your donations. And I thank you for watching.