Introduction
Let's go. Good morning, good afternoon, good evening, no matter where you're coming from. Hope you all are doing well. Let's go. What's going on, everybody? Welcome back. Welcome back. You came back. We learned about encapsulation, abstraction, two really hard concepts, and you decided to come back tonight for the inheritance and the polymorphism. I'm glad you're here. I'm glad that you decided to show up again. These are meaty topics, but we're going to get through them together. Hey, we got a lot of fun stuff tonight. Lots of really good examples going to make OOP make sense. We got two more big things to get through.
We'll do more review on Thursday and Thursdays are really exciting class too. Woo. Folks, you believe it? We're halfway there, folks. We're halfway there. This is officially the halfway point. We're there. We made it. Let's go. Uh, so I'm going to put 10 seconds on the, on the clock here. Cause we need to celebrate a little, just a little bit, a little bit. Thursday's gonna be the big celebration. I see a little bit 10 second warning Congrats, everybody. Halfway there, could you believe it? Think about all the stuff you're able to do.
The things that you now know. HTML, CSS, JavaScript, variables, conditionals, functions, loops, arrays, objects, properties, methods, constructors. What? What? Let's go. That's wild to think about. I could sit here and rattling off all these things all day long that you all now know halfway through. Congrats. It's a huge accomplishment. We're going to celebrate on Thursday because we got some important stuff to get through. We got to finish out OOP, and then we could celebrate with the best of them. We have come so far. So lots of fun stuff to get into tonight. Glad you are here. You know how we do.
You know how we do. We got a little bit of things to talk about. 100 devs related, we're going to review objects. If you missed the last class, that's okay. We're going to review the basics of encapsulation and abstraction together. And then we're going to move into inheritance and polymorphism, depending on how it goes, depending on how we're feeling. We might have some time left at the end. We might do another round of kind of working in groups, but we'll see how tonight goes. We'll see how we're feeling. These can be meaty topics that can make our brain put in some effort. So let's see how we're feeling towards the end. But you like as we like as we always like to do I'll let it off with some questions. So the The the questions is of the day is what is your favorite character from television books? TV whatever that's the question of the day I was it's off with a question give folks a few seconds to get in here that are running late from work Got Goku Hercules We got a lot of Simpsons characters, Kermit Optimus Prime, Sailor Moon, okay I see you. Now see if you're from Philly and like you woke up early, I used to wake up mad early to go to school because I had to travel pretty far and I would wake up and every morning Sailor Moon be playing at like 530 in the morning.
So I wake up and watch Sailor Moon every day throughout high school. Mulan, eh. We got a lot of interesting ones. Riley from the Boondocks, okay. Pooh Bear. Hulk, Punisher. We got Michael Scott. We got a lot of good ones. Batman, Ron Swanson. I feel you this is good. I like to see these these are my favorite types of questions I like to see what y'all are into First Buehler Leah I get out of here Jack Sparrow, okay Uhtred son of Babenbrook. Oh No Uhtred son of Uhtred All right, thank you all for sharing I think my favorite all my favorite characters come from I think Alice in Wonderland So Cheshire Cat, Mad Hatter, a couple other fun ones. Dodo, yeah. All, I think those are my favorite characters. All right, thank you for sharing.
Folks have other questions. Questions before we get started. I always like to do one or two before we jump in. Can we do more rock, paper, scissors gambling? We'll have some gambling. Thursday is gonna be a gambling show. I don't wanna say gambling. We're gonna YOLO some points on Thursday. How are you? I'm doing good. It's been a good week so far. Any advice for a phone interview? My general advice for interviews is always know what you're going into, right? You should never walk into your interviews like an accident. Always ask.
Hey, is will this be a behavioral interview? Will this be a technical interview? Right. And always know what you're walking into in different. Hey, what's going on? Always know what you're going into. Right. So always ask. Don't ever be afraid to ask what type of interview you're about to go into. We do have something called the bank. So you can do you can do exclamation point bank. And it's all the questions that we prepare for behavioral interviews. And then all the questions we prepare for technical interviews. And so if you know, you have an interview coming out, where I saw a couple of folks that they have some interviews coming up, definitely start working through the bank now, uh, we'll have some time when we'll officially start working through the bank together in like two weeks, like not next week, the week after, um, but you can take a peek at it now if you, if you get in some interviews early, yeah. How many paid clients do we need by the total end of the cohort that's up to you I asked for one I Asked for one and some folks really run with it I have some folks that'll just that'll just take off and they'll freelance for the rest of their lives And some folks will just get the one and that's it that's up to you If we if we were just dealt a major life gut punch and fell behind, how would you deal?
How would you rally when your energy feels gone? First it is 100% okay to take a break Right. It is 100% Okay to take a break This is a marathon not a sprint. We all have things they're gonna hold us back some more than others and that's okay It's not really being held back. You're just taking a pause. The one thing I ask folks that take a break, that take a rest before they continue on to get the energy up, is always set a date that you're going to return. Put it on your calendar, put it on the fridge, put it on your wall, and say, I'm gonna give myself a week. And then once that week is up, here's the date I'm gonna start, and here's the very first thing I'm going to do. I'm gonna do a Code Wars. And literally put that on on on May 12th. I'm gonna come back I'm gonna do this specific code wars and that's how I'm gonna restart Folks that sometimes when they take a break and they don't have that date of return plan They don't have the thing that they're going to do then It just becomes a little bit too easy to not pick it back up So 100% okay to take a break 100% to listen to your body to listen to your emotions to listen to your feelings and say I Need to take some time to rally 100% Okay, set a date to come back and then make sure you come and get help right joining the discord getting help in the Discord communities joining the catch-up crew find your people and get that support to keep moving through All right I have a business degree and been following along with the boot camp. I was advised by my coding friends I was in network with to not kill myself learning to code but to apply for scrum master and agile delivery lead positions Are these positions realistic for someone that is extremely tech savvy but transferring sectors? How hard is to gain entry would you expect not my zebra not my zoo? I'm laser focused on getting folks software engineering jobs. It's not something that I I don't like to speak on things I don't know about right have I had folks get scrum master positions Have I had folks go and get those type of jobs?
Yes, but I honestly don't know the best pathway into those types of roles. And the thing I do know is that my students that take. Like in the realm of software engineering roles, but not software engineering roles traditionally get paid less and have a stunted career growth. So I'm talking about things like QA or other things that are, that are kind of tangential to software engineering, but not the whole kit and caboodle, so not saying that there there's nothing wrong with those jobs. It's just that my students that take them typically stay there even if their goal was to eventually become a software engineer and they Stay there with typically a little bit lower of a salary. So that's why I don't I don't put my energy into it I put my energy into software engineering and that's that's kind of what I figure out dealer a congrats on the interview Yeah, and different said the difficulty with positions outside of software engineering roles is that you usually stay there that is 100% true Mastermind is back. Hey, all right. We got to hit him up I got it. We got it. We got to make we got to make that happen. I'm really glad that they're back. We needed them Y'all had a meetup in Harrisburg, let's go congrats. That's huge guys. You got to meet some folks already folks Thank you for the questions. Let's jump into we got a lot of stuff to cover tonight If you're following on along on YouTube, please go ahead and like comment and subscribe Only about 60% of people that watch the YouTube channels are actually subscribed.
So just just click the click the button, please. Thank you. Alrighty, if you haven't checked in, go ahead and check in, please. If you are following along, please check in, like and retweet the tweet. You can do exclamation point check in to get the actual link. You have to click the link. You have to go to the tweet. Give it a like. Give it a retweet. It helps folks find our community, not pay tens of thousands of dollars is to learn what you have already learned. Let's just stop here for a second. So I used to teach at General Assembly, right? I'm still distinguished faculty there, but the program I taught the most was a nighttime course that was all front-end web development, right? Front-end web development. We learned HTML, CSS, and the JavaScript up until like maybe two weeks ago, three weeks ago.
So like you're already past that and that course cost $12,000, 12,000, yeah, yeah. Now they had smaller part-time courses, right? that were like in the three to 4,000, but the skills you already have are like 90% of that, that 12K course, which is wild. And so, that blows my mind that we're doing this together and we're all still here. If we were to calculate the money saved, it's just astronomical, astronomical. All right, now, so please go ahead and like, retweet, help folks find us, help folks get to try it for free. For some folks going to an in-person bootcamp makes sense. They want to put the money on the line, show up. But for a lot of folks, they want to test it. They want to try it. They want to see if it's for them. If this coding thing could be an actual career for them. They can come, they can watch all of our videos for free. They can join our discord, 31,000 plus strong to get the help that they need as they're on that journey and they can do it for free. So please, a like, a retweet, just so folks can find us and not have to dish out those funds.
All right, now, if you are new around these parts, we have a newsletter. You can do exclamation point newsletter. And when you do that, you will get access to the emails that go out before every single class. It's often a place where I share the most information about the upcoming classes. I'll put links in there early. And sometimes there are super secret raffles like there was today. And so today there's a super secret raffle and that raffle was for VIP folks, for VIP here on Twitch. And the cool thing about VIP is you get some perks. You stand out, you look special. You get a special little icon here on Twitch, and you can share links too, which is pretty cool. So VIP has value. If you open up the newsletter, I would have saw a newsletter you could have entered in. We had over a thousand people that filled out this newsletter before class started. So I'm gonna grab all the names. Go down here.
Until class started. I see some of you kind of doing it right now. I'll count it. I'll count it. You open a newsletter, folks, it pays to open a newsletter. You get cool stuff. Let's go ahead and do a name picker. Name picker.net, here we go. This is for VIP folks. Why does it say a thousand? There's over a thousand. Oh, maybe there are some repeats. Oh, no, I think I just didn't grab all of them. All right, I see all you sneaky folks trying to get in. I'm gonna grab some of the sneaky folks.
There we go. All right, over a thousand. Three, two, one. Charlie Workdoer. Charlie Workdoer, you got VIP coming your way. When we take the break, I'll VIP you up. Maybe I can do it with just like a chat command. Let's see. I can do it with just a chat command. All right, Charlie Workdoer, you got VIP, congrats. Use it wisely, please. Use it wisely. Stand out. Flex on your friends. Be a flexasaurus.
All right. Congratulations. It pays to open the newsletter, folks. You heard it here first. Networking comes back next week. Can you believe we're done? We're done with April? Like April just like what I teleported through April and we're at we're at May All right, we're in May. So that means next week the networking has to come back folks We're getting to the point where you know, some stuff, you know, some HTML, you know, some CSS, you know some JavaScript's All right You're gonna get closer to the hunt. We got to. We got to start bringing back that networking so that we have a basis of folks to reach out to when it's ready for the hunt. Let's bring it back. Let's dust off the cobwebs. It's gonna take a little while. We're getting close, folks.
We're getting close to the hunt. So we gotta bring the networking back starting next week. Deadline for clients is this Thursday. I want to see your your signed contract I want to see if you did a client alternative if not Meaning that you volunteered or signed up a contract to volunteer or you contribute it to free software That's Thursday folks and then Sunday Sunday we come back Sunday we come back for the get Bowl. We had a lot of folks that had questions about get on Sunday's office hours So I decided to just do a full day of git and github and so Sunday we're gonna take three plus hours Oh, no, it's Monday's day fig newtons. I met Monday. I met Monday I met Monday because Sunday is Mother's Day. I got to fix the slides Let's let's can't let's do this real quick Monday Monday 6pm we're going to go ahead and do a get bowl it will be recorded it will be And what the heck is a get bowl folks are asking? We're just gonna go through get and get hub from beginning to end. We're gonna learn how to add, to commit, to push. We're also gonna learn how to pull. We're gonna learn how to fork. We're gonna learn how to clone. We're gonna do all of the fun stuff. It will be recorded if you can't make it, but joining live is always the fun part.
All right, don't forget to call your mom on Sunday to argue. All righty, speaking of wonderful things that we can review including Git and GitHub, MyinWolf has pinned an amazing list of all of their review streams in the Ketchup Crew channel. You can click this link. I'm going to copy the link address. I'm going to spam it in chat real quick in case you need it. There we go. They have listed out all their review classes. So if you need object review, you got it. You want to build a word or clone, you got it. If eloquent JavaScript gave you too much trouble, you got it. You want to look through that object calculator project, you got it. You want to look at some APIs, you got it. So MindWolf is a beast doing all these review streams as part of the stream team, please give them some love. Like they're taking time out of their day to review all the things that the people in our community need the most help with. It's wild.
They're literally giving up their their nights and weekends to help folks review and they're doing it live. You know how hard it is to do it live where people can scrutinize and see everything you're doing. And they're you know what? Come and get a little close. they're really fucking good at it. Like they're really good. So if you're not following Mayan Wolf, you're losing out on some great content, some great review. I love that they listed it out so you can pick out and cherry pick the things that you want. Mayan Wolf is doing a week delay so they do their live stream. Make sure you give them a follow so you can see when they go live. And then a week later they put their stuff on YouTube. Amazing educator, somebody that's stepping up to make sure you all get the help that you need with some of the meaty topics that sometimes I can't take more time to review, Mind Wolf is going through it. Cool. Now, some people are like, wait a minute, how come I keep missing Mind Wolf streams or hello, it's Rufio streams, why do I keep missing them? Well, it's because you haven't turned your notifications on.
You gotta turn your notifications on, folks. If you go to the stream team channel in the 100 dev section of our Discord, right? You go to the stream team channel. You go to notification settings. Just click all messages, please Click all messages, please folks in the stream team only posts when they're going live with a hundred devs related content or Coding related content, right? So we promise not to Spam the the notifications. You're only gonna get the bangers only folks, right? Stream team is out here making it easier for you to learn, for you to pick up things, to get the review that you need. So just go ahead, stream team, right click, notification settings, all messages. I'm just gonna give everybody a few seconds to do it. I'll wait. Just a few seconds, pull up your Discord. Stream team, notification settings, all messages please. Give everybody a few seconds to do it. Is it normal I don't feel comfortable We'll making cool layouts.
Yeah, are you a designer? If you're not a designer, I don't go make cool layout All right, some folks are saying I don't see it if you still have a gray name on discord It means you haven't joined the hundred devs on discord. You have to go to the join 100 dev section. Click those emojis and So many more channels open up for you including the stream team channel cool My wolf says I can't wait to see you on my next stream. Hey, let's go What was the song you played at the beginning of your crafting your story stream that was the outro On Meek Mill with little snoop Yeah, oh Thank you for setting your notifications as all messages. It really does help folks. It does really help you see those notifications coming through. We only put it when we're doing 100 devs content. So I really appreciate you doing that. It'll help you never miss a stream team stream. Thank you. And with that, we have one new stream team member to introduce. We've re-rated them. They've been a long standing member of the 100 devs team. It's finally time to make it official.
Fox is joining the stream team Congratulations. Thank you so much for joining Taking their time to help and work and and and and kind of bring the community together And there's a couple really big things. I love about Fox's streams one They're starting at 11 a.m. On Friday. So Friday will be their first official stream as part of the stream team They're tagged up on discord They're gonna get tagged up on Twitch and their first stream is gonna be at 11 a.m. On Friday, which is amazing because a lot of folks Aren't on our like Eastern Standard Time that classes start, right? So we wanted to start adding some folks to the stream team that are at different hours So that folks that are in different parts of the world They still get to interact and be part of this community and grow with us But but Fox is doing something really interesting They are streaming work together sessions. So if you want to come and just work together with other folks and you don't want to do it alone, especially during the day, definitely check out Fox's stream. They're amazing. We've rated them once before, but now it's official. They're part of the stream team. You're going to have some fun. You're going to learn some stuff together and it's going to be awesome. So super, super excited. This is one I've been wanting for a long time.
Congrats Fox, join the stream team. Joda, SAA, thank you for the gift of subs. I appreciate it, thank you for that. Woo, we're bringing the heat, folks. We got a lot of, this is stream team. Thursday, I got some other big announcements to make too. I'm really excited. We are really, really lucky to have the folks on the stream team that we have, and I'm telling you folks, I got a vision. We're going for it. is the best place to learn how to code live online. I want every single day to be a 100 devs person live that you can join, that you can hang out with, that you can code along with, that you can get help and support with and be a part of this community. So Friday, 11 a.m., I'll be there. I'll see you there. Welcome Fox. Hey.
All right, chat. What the heck are objects? What the heck are objects? What are objects? Everything. Everything. Oh, why pin mine? Oh yeah, it's a thread. Can I pin threads? I'm not sure if I can pin threads, but it's a thread in the catch-up crew. Cool. Someone said, how can I see all of the members of the stream team? If you're on Twitch right now, underneath my name, it should say like 100 devs. That's our stream team. If you click on it, you can see all the other members of the stream team.
Cool. Yeah, they are everything. Objects are everything. They are a collection of variables and functions. And when these variables and functions are tied to objects, we call them properties and methods. Now, we've been using objects for a while now. We've been using them just to build regular objects. We've been using them as part of APIs. We've been using them, we've been consuming them. Last class in this class is trying to understand why they matter, like why we actually would want objects in the mix, It's like why we'd want to use our objects in regular day-to-day life. And so we were using them, now we're trying to understand them, and then the last and biggest level is to build with them. And this is something that I really want you to take to heart every single time you're learning something new, especially at 100 devs. It's a lot of times it's just worthwhile to get in there, use it first, see how it works, tweak it, play with it, break it, then try and understand it. Try and fill in the gaps in your knowledge. And then once you have a rough idea of it, start building because in that building is how you'll solidify the concept, right?
So use, understand, build. It's something that has changed my life when it comes to learning how to code. It's something that I try to infuse in what we do here at 100 Devs, but it's something that you can take and run with for everything that you're trying to learn. Just get into the muck, use it, then try to understand it, then try to build other cool stuff with it. All right. I mentioned this last class on last Thursday, uh, but we're finna make some nerds angry, especially tonight, especially tonight. We're taking some of the media's probably hard to under hardest to understand topics and we're trying to bring them back down to earth, right? make them make sense, use real words, like real words that we can understand, no big words, because we treat them as disrespect, right? Come back down to earth exactly and have it make sense. Now, when you do that, do we lose some of the nuance of these terms? Yes, of course, but we don't care about that. We're trying to get an understanding and understand why these things are important. And who cares if you can memorize the definition of polymorphism, if you have no idea why you use it or what it brings to your code. And so that's what we're spending a lot of time on. We're spending time understanding the why, wrapping our heads around the different pillars of object-oriented programming, and we'll have time to see the more nuanced explanations as we go further.
So if you're a code weenie and you're watching tonight, don't worry, just like Thursday, I put a little little asterisk next to any of the things that aren't a hundred percent all the way there So that you can just close your eyes. You can take out your headphones You can walk away for a few minutes as we Actually explains the way things in a way that people can understand And then once you see that asterisk disappear, you can come back you put your headphones back in And you don't have to get angry or upset All right Cool We talked about objects as being a collection of variables and functions These variables and functions we call properties and methods and they represent the attributes and behaviors of things in our program. When we think about objects, I like to think about a physical object. We need a code weenie emoji. I think so, that'd be funny. I like to think about physical objects. I think about a stopwatch when I think about objects. I see the stopwatch as having properties like color, shape, I see it having a brand property, a model property, and I see it having methods like start, stop, split, reset. If you're new, we've been going through these objects for a while now, so this is definitely a little bit of review in the beginning. All right. At first, we started creating object using what? What type of notation is this to create an object? Box it. Thanks again. Everybody this community means the most to me up and then we got blown out box it Thanks again.
Everybody's community means so much to me and I will do my best to make you guys proud We're already proud you don't to make us but we're already proud but super excited to have you as part of the stream team Let's go. Yeah, we're using literal notation. All right literal notation When we use the curly braces, we're using literal notation and we're adding properties and methods using what kind of notation here We're adding properties and methods using what type of notation? Yeah, dot notation, exactly. Awesome. And we're adding a property here, but a method here. How do I know that I'm adding a property here versus a method here? What's the difference? Yeah, one is a function. Yeah, so whenever you're using a function with an object, we call that a method, nice. All right, what if we want to make a lot of objects? What can we use? We want to create a lot of objects. We want to create a factory of objects. What can we use to create a lot of objects?
Yeah, we can use a constructor. And that constructor enables us to spit out objects. We can make an object that has all of these properties and these specific methods tied to it. And any object that comes out of this constructor will have all of these methods, all these properties, and these two methods. And then we saw, well, we have this constructor function, but with the newer versions of JavaScript, we got classes. And classes were what on top? Classes were what on top? Bob was guessing, Bob was guessing before I even said it. They knew what was coming. Yes, it's the splenda. Is the syntactical sugar that goes on top right? It's still Prototypal inheritance underneath and we'll have a better idea what the heck inheritance is like after tonight's class But it's still prototypal inheritance underneath. We just have a little bit different of a syntax that Will help folks that are transitioning from other languages and helps us do a couple neat things as we get in tonight's class Tonight we're covering inheritance and we're gonna see how this class syntax gives us some cool stuff to Really help when it comes to the inheritance game well Alrighty But why Why do we put all this effort we already have variables We already have functions we could just make a whole a Whole program using variables and functions. Why the heck use objects? Why do we need these like factories these constructor these classes the gift is loud?
The good good to the classical inheritance I Love that I'm seeing people say like encapsulation already. Oh that makes my heart feel good Why do we use objects well Well, what if there was a system, nay, a paradigm, nay, a set of rules, an agreed upon structure to our code that made it easier to add new stuff, made it easier to read through what was already coded, made it so that you were not afraid to make changes. We can make changes and go to sleep at night. You see the warning what if there was a system a paradigm instead of rules well OOP baby, that's what we did. But if we had a system, a paradigm, a set of rules that made it easier to add new stuff, made it easier to read through what was already coded, made it so you weren't afraid to make changes so you could sleep at night, that's OOP, object-oriented programming. We use these objects to make our lives easier, easier, easier. All right, let's take a look. Let's see some code. Ugh, disgusting, ugh, ugh, disgusting. I do not like this code. All right, let's look at this code. With this code, we have some things here. We have some variables, our hourly rate, our hours worked, our tax rate. We have a function to help us calculate profit. and we actually calculate that profit and store it in a variable called profit, and then we're console logging profit.
So I have a question. Is it easy to add new features, Tina's been here. Why is that so funny? Is it easy to add new features and functionality? Let's take a look. Here we go. I just added a way to hold money for taxes, right? So we had our calculate profit. Now we add a new function to hold for taxes. We have a new place where we're holding for taxes. This is one thing that we've added. And do you feel comfortable with what we added? Do you feel like you understand what's going on? Like we could sit here, a spaghetti monster. Exactly.
We could sit here and we could figure it out. Right. It'd be like, all right, all right. So hold for taxes needs profit made. So if we come down here, we can see that, all right, profit made is actually profit that was passed in. Profit was passed in here. Profit is using hourly rate. Hourly rate's coming from here. Hours is coming from here. Tax rate's coming from here. And then all that's being passed in to calculate profit. So hourly rate becomes rate. Hours becomes number of hours tax rate becomes taxes that goes in we calculate the rate the number of hours to taxes That value gets spit out and get spit into where the function was called down here. It goes back up the whole four taxes No, no, no No, God, no, God, please no, no, no, no. Right?
Like we could sit here and figure this out, right? We could sit here and figure this out. We could, we could, we could, we could do Leon's playing Madden tonight. We could sit here and figure this out. We all could read this like very procedural code. This code works, we can figure it out. But all of our parameter names are named something different. There's nothing that would stop, if we were working with another engineer, there's nothing that would stop them from adding a function that also had different parameter names. It's really not, like, there's nothing, it's just too much. It's bad code. We're bad as we write bad code, but this is too much, right? It works, we can figure it out, but we wouldn't feel comfortable really kind of like understanding what's happening. Can another developer just like look quickly and get the gist of what's happening? No, they're gonna have to spend too much time doing what we just did, like tracing everything through. Whenever they wanna add stuff, they have to go through that whole maze of figuring out what stuff is, nah, right?
And as we kind of like keep looking at this, like if I made a change, Like if I'm looking through this, like, can I spot changes? Like I just, I just made a change. Could I, could I see what, what happened here? No, it would take too much time. We're going to see, we're going to be sitting there trying to calculate all these things. We're going to be like, it's just too much, but does it work? Yes. We could just write variables. We could just write functions and get our programs to work, but it's not maintainable. There's too much stuff all over the place. And we can't really make changes without losing sleep at night. If we make a change here, is it affecting something further down in our code base? I would have no idea. If I was working with another developer and we were both making changes at the same time, there's a really strong chance that our changes would break each other's code. And so we need to figure out a way to not be stuck with this, but with something that can help combine our data and functionality, because when we're looking at it, we noticed that there was some data, right?
We had our hourly rate, we had our hours, we had our tax rate, and we had our functionality, like the ability to calculate profit and the problem was the problem was there is no connection right we had data we had functionality but there was no connection and so what if there was a way that we could fuse our data to fuse our functionality together and get all the benefits that we talked about last class. So we got to do the full dance, right? We got to, we got to, right? We got it. We got it. We got to fuse the data and functionality together because when we do that, look how clean it looks. Oh baby. Look at this. Look at this, would you just look at it? Beautiful. Galza, right? It looks good, right? That's clean, that's clean, that's fresh. Look at it, look at it. We have one object where the data and functionality is combined, right?
So when we're talking about the hourly rate, we're using the this keyword. So we're not talking about some other hourly rate. We're talking about the hourly rate that's part of this object. When we're talking about the hours, we're not talking about some other hours, we're talking about the hours that are a part of this object. When we're talking about the tax rate, we're not talking about any tax rate. We're talking about the tax rate that's a part of this object. And so is it easy to add new features and functionality? Yes. It's easier to add the features and functionality because we're all using the same language. We're using all the same variables, right? So there's no, there's not anything getting away from us. We're not having all these made up parameters. And if somebody wants to get the hourly rate, they can clearly see that there's already an hourly rate set. So we don't have to worry about outside global variables. We don't have to worry about somebody coming along and messing with it.
All of the data and the functionality is fused together in a way that makes it way more readable and easy to make changes. So now when we wanna add a calculate taxes held, we know what data we're working with. When we go to calculate taxes held, We're dealing with the same data that we were dealing with when we were trying to calculate profit This makes it way easier to keep adding new bits of functionality without worrying if we're using the wrong bits of data our data and Functionality are fused together Now can another developer look at my code and understand what's happening Can you quickly look at this and understand, all right, I'm calculating profit and I'm using these values here. You know, the developer look and see what's happening. Do they have to look anywhere else in my program to see how I'm calculating profit? They have to worry that somewhere else, a hundred lines down in this file, something else is going to change. No, they know exactly where to look and they can understand what's happening. They don't have to worry that there's something loose somewhere else in my codebase that's going to make it hard for me to understand What's happening? Can they make changes without losing sleep? Right. I made a change here. I changed the check rate the tax rate from 0.35 to 0.4 Am I gonna lose sleep that all of my functionality no longer works? No, I didn't have to I didn't have to worry because I was already using the tax rate from the object. So I don't have to worry about any other bits of my functionality breaking because I already know that functionality, right? I already know that functionality is using the same set of data.
The data and the functionality are fused together. So this fusion of data and functionality Functionality into one object. You see the warning this fusion of data And functionality into one object well Yeah, there's no music I know I can hear it and hear it as encapsulation, encapsulation, encapsulation. Let's get, let's there's your warning nerds. We had to find my pen again. You're nerds. You're warning. Close your eyes, take your headphones out. It's okay. The process of storing our function, our functionality, our methods with their associated data or our properties into one thing, aka an object, that's encapsulation. Using our data and our functionality together into one thing is encapsulation. Cool. So with encapsulation, is it easy to add new features and functionality? Yeah, yeah way easier cool Let's take a look at the current best Prosumer Espresso machine on the market All right. Now we spent a little bit more time on this last class So this is a little bit of a review right a little bit of a review We're going to go through encapsulation and abstraction a little bit quicker so we can get to inheritance of polymorphism.
If you're brand new, you're joining us for the first time, we're running a 30-week software engineering bootcamp and we're halfway through, so this review is going to be a little bit faster if you're just joining us live for the first time, so just keep that in mind. Also, if you missed last Thursday's class, we're going through this a little bit faster, but we're going to recap at the end. Before we leave tonight, we're going to recap and we're going to talk about the why. We just saw that it was easier to add new features and functionality when we use encapsulation But we're still gonna get to even more of the why of encapsulation when we recap towards the end of the night as well All right. Here's the gaja classic Pro. It is the best prosumer espresso machine on the market Makes beautiful espresso has one glaring big problem HssoftwareengineeringA, thank you for the prime. Yeah, the wand, it's hot garbage, literally hot garbage. The wand that you see on the right-hand side, that wand is used to add texture to your milk, right? You wanna have beautiful lattes, right? You need texture in that milk, and the steam wand is there to provide that texture, But it doesn't it does it's it's not good. It's not good. So The gaja classic pro has a boiler right you put water in and there's a boiler and That boiler creates steam right, it creates steam that steam can go through your portafilter and make delicious expresso and And that steam can go through the wand to help add texture to your milk, right? so it heats water for espresso and Steam for your milk, right? So it heats that water to make steam for your espresso and your milk but the steam wand sucks and Since the steam wand sucks What should the engineers do if the steam wand sucks? What should the engineers do?
They should fix the wand. Fix the wand please. Just fix it. Fix the wand. All right. They should fix the wand. now do the engineers have to redo the water boiler do leon you're really trying to reach a certain audience see we're all here to get the money and once we get this money and you're ready to upgrade your coffee experience think of me Think of me once you once you've run into big bucks and you're like, you know what? Time to go expresso Think of me All right What should they do they shouldn't they shouldn't get rid of the the water boiler that's making the steam They they they shouldn't get rid of It has to be an aspirational stream, right? They shouldn't get rid of the portafilter that makes excellent coffee. They shouldn't get rid of the buttons, right? They shouldn't get rid of the buttons. They should just fix the freaking wand, right? They should just fix the wand. Did they get rid of the water boiler? No.
And when they're fixing the wand, do they really need to worry about the portafilter? When they're fixing the wands, do they really need to worry about the water boiler? No, the engineers can just fix the wand. They can focus on just the wand. They know that when they make changes to the wand, they're not gonna break everything else. The wand has crappy two holes in it. It should just have one, right? And so if the engineers just focus on the wand, that saves them a lot of headache, a lot of trouble. They don't have to throw out the whole espresso machine. They can just focus on the wand So we can say as they're fixing the wand that the water boiler. Well, that's probably Abstract it Know what I'm saying? No saying they don't have to worry about it. It's abstract it away from their concern All right Let's think of another example. When we get into our car, if you have a car and you get into a car, if you've ever driven a car, when you get into a car, do you think about how the cylinders are firing? Do you think about the way that the air to the fuel ratio inside of your engine?
Or do you just press like the button or turn the key and hope it goes? All right We got we got some we got some car weenies in here Leon I care about the exact ethanol ratio in my in my gas I gotta make sure that those cylinders are firing on the right times I care about it all the time Leon I get in that car and if I'm not thinking about it ain't gonna go For most of us that are not car weenies, we turn a key or press a button. Who am I kidding? I press a button, baby. You know what I'm saying? I was getting the car pressed to buy and got no keys. Get that money. Uh, I press a button, my car turns on, right? Car turns on and, uh, I just use the steering wheel and I use the pedals. I ain't thinking about anything else. the whole rest of the car is abstracted, right? The whole rest of the car is abstracted. I only care about my interface. I only care about my interface. My interface is my steering wheel.
My interface is my pedals and my shifter. That's it. That's my whole interface for the car. I don't care how the fuel's making it from the gas tank into the engine. I don't care about the oil doing its lubrication. I don't care about the intercooler. I don't care about anything that's happening inside of that car. I press the button and I go All right, that's it I have a very limited interface I everything else is abstracted Everything else is abstracted Away from me. All right. Let's look at some code Uh-oh Uh-oh, so I made a calculator I made a calculator for our agency and the invoices can make the clients can get their invoice but they can also see the hourly rate and they can actually to see the amount of profit that we made. And that's bad. Like we don't want them to know the hourly rate. We don't want them to know how much profit we're making. We wanna make sure that they feel like they're getting all the value in the world that they got us where they want us, right? And so we can't let them see the hourly rate.
We can't let them see the profit. We just want them to be able to get their invoice. And right now, if they were smart, they could see, all right, how to get the profit. and they could see how to get the hourly rate, right? And that would be there for them to see. We can make a quick change. And we're gonna see private properties and methods later tonight. We're gonna see that when we get to inheritance. We're gonna see this way of doing this right. This is a nerds violently shaking scenario, by the way. So I just made a few quick changes here. I set hourly rate and the calculate profit to Local variables. They are no longer this that means they're no longer bound to the what they're no longer bound to the what? Yeah, we're gonna get into getters tonight yeah getters and setters Yeah They're no longer bound to the object like the object that gets spit out of this this constructor is no longer going to have a rate property or a calculate profit method. However, right?
However, inside this constructor, we can still figure out the rate. We can still calculate the profit. However, when our user tries to get the hourly rate or the calculate profit using that dot notation, right? They get that dot notation. They can't get it because it's not bound to the object. They're just variables that are used inside the function. There's no way for those two things to come out of the function. They can't use that dot notation anymore. This is different, right? This is different when we bind them to the object with this. Remember objects come out of this constructor with the this keyword, they're gonna have that tax rate. They're gonna have that calculate profit. But since we set them, right? Since we set them to the hourly rate and the calculate profit are set to local variables. They only exist inside of this function.
Anyone that tries to access them outside of the constructor, they don't exist. They are scoped inside of this function. Cool. Hey Rockets. All right, y'all throwing some big words at me right now. Throwing some big words at me, and I'm gonna treat them as disrespect. We're not there yet, we're not there yet. Cool, so we were able to, we were able to abstract some stuff away from our client. All right, so my client can still get their invoice, but they can no longer see our hourly rate. That's beautiful. They can still do everything they need to do to get their invoice. Our invoice needs to calculate how much to charge them, but they can no longer see our hourly rate. The water that was boiling in our Gaggia Classic Pro espresso maker, that steam that was generated and how it was generated, that was abstracted away from the wand. The wand didn't care about how the water was boiled and the steam was created, and an engineer working on fixing that wand does not care. They can focus just on the wand.
The hourly rate was hidden from our client. They could still get their invoice, but the hourly rate was only for our use. They couldn't get it. The complex or unnecessary details are hidden. They are hidden. This enables you to implement things without understand or even thinking about all the hidden complexity. If you were an engineer working on the gauze a classic pro steam wand You could just work on the steam wand and not have to worry about anything else This idea of hiding the complex or unnecessary stuff Makes our code be able to be in smaller more manageable pieces When we are working on a huge team with other engineers We wanna be able to take one bite-sized piece of code, work on it, fix it, break it, do whatever the heck we want and not worry about the rest of our code base. If we had to worry about the rest of our code base whenever we are making changes, that would be a bad news. Hey, Baringsphoto, hey, what's going on? How you doing? Thank you for the gifted subs. Hope you're doing well. Hope you're doing well. Thank you for being here. Good to see you again.
Hope you're doing well. Yeah, smaller, more manageable pieces of code. And it enables us to do stuff once. If we are making a new steam wand, we don't have to remake the boiler. We don't have to remake the portafilter. We don't have to remake the buttons. We can just fix the wand. Enables us to do stuff once. Same thing with our code. If we abstract out our different pieces, we can just do stuff once and use those pieces whenever we want. We can look at our code here and we can reuse the calculate profit whenever we want. We don't have to redo the calculate profit every single time we need it. When we try to figure out what we're going to charge our client or how much we have to save for taxes, we don't have to constantly be remaking these bits of functionality. We can do them once and only use them as needed. So this idea of hiding complex or necessary details, implementing and doing stuff once, making more manageable piece of code.
Here's your warning. So Let's go Oh, had to show some love after getting a job. Hey, I appreciate you showing the love. Thank you for being here. Congrats on the job. That's huge. Thank you for the gifted subs. Alrighty, folks. I think it's important for us to just summarize real quick and then we're gonna take our break. Abstraction is hiding, welcome proxy. Abstraction is hiding the details, just showing the essentials. It makes our code simpler, more predictable, more manageable, and when we come back from break, we're gonna live my vegan dream and start a farm. So if you're new around these parts, we'd like to take a five minute break at the top of the hour. If you're able, please get up, move around, hydrate, let your eyes focus on something that's not your screen. Come on, folks.
Grab some water, hydrate, kiss some babies. When we come back, we're building a farm. You You You You You All right, folks, welcome back, welcome back, let's go, let's move on here. All righty. All righty. Feeling good. We did a little bit of a speed review of encapsulation and abstraction, making it so that we can to have easier to read, more digestible code, where we don't lose sleep over making changes, where we can hide the unnecessary stuff so we can focus on the things that we want to build. With encapsulation and abstraction, we can get all those things done. Maybe nugget, maybe. All right, folks, let's start a farm. Let's have some fun, let's start a farm. All right, so here I have an animal class and we can see in this animal class, we have a constructor that gives all the animals on our farm a name and it gives them the ability to speak. Right. We have the ability. Oh no, Leon bought a farm.
One day, that's like a, that's like long-term dream. We have the ability to have an animal class that has a name and the method of speak. And so what we're seeing here is that we want to have a farm and we want every single animal on our farm to have a name. Cool. Now, what if we wanted to have a bunch of different animals on our farm? Well, we can do something called extending our class. So this is where the objects start to get a little bit more fun. So, Leon running a software engineering farm. So my wife really wants to start like an animal rescue, like, like, uh, uh, specifically a pit bull rescue. And so she had this really funny, like, there's the pit bulls and parolees. She wants to do like pit bulls and programmers. So it's like an onsite coding bootcamp where instead of like paying to go through the bootcamp, you take care of the pit bulls while you're there. And so like, it's like a, like a work share. Um, I did a lot of like work shares growing up. And so that's, that's, that's like something we've been thinking about for a while.
Somewhere, somewhere along those lines, but I think it'd be funny. I think it'd be cool. All right. So, we can extend our classes, which if you've done the reading, you might've seen so far, right? But this idea is that we're gonna have all these different kinds of animals on our farm. And so here, the first animal we could have is this lovely dog. And dog extends animal. So we get all the wonderful features of the animal class, but we can also add the stuff that we care about when we're talking about dogs. And so let's think about dogs all needing a breed, right? A breed. And so we wanted to have our animal class for all of our animals. And we wanted to have a dog that extends that animal class and adds the stuff that just the dog would need. So here you can see that we've added the breed because maybe not all animals have a breed. All right. So the MDN has like this little quip that's kind of hard to read and understand.
But it's something that I want to spend a little bit of time reading and then see if we can like pull it apart and make it make sense. Let me just read it once and then we're going to come back to that code I just showed you. We're going to walk through super all that stuff. Let me just let me let's just read this real quick. If you find yourself starting to create a number of objects that have similar features, that's what we're about to get ourself into. We're creating a bunch of animals, right? We're creating a bunch of animals, but we know that we're going to have some animals that are very similar, like all of our dogs are going to be similar, all of our cats are going to be similar, all of our cows are going to be similar, all of our horses are going to be similar, right? So if you find yourself starting to create a number of objects that have similar features then creating a generic object type to contain all the shared functionality and inheriting those features in a more Specialized object types can be convenient and useful let's go back all right we already know we want to create a bunch of objects we know that we're gonna create a bunch of animals we're gonna create a bunch of animals we're gonna create a bunch of animals so we're I'm with you there we We need to create a, we're building a farm. We need a lot of animals. All right. Now we know all of our animals. Every single animal on our farm is gonna have a what? Every single animal on our farm is gonna have a what? A name. Every single animal on our farm is gonna have a name.
All right. So then creating a generic object type to contain all the shared functionality. All right. So the shared functionality that all of our animals are going to have is a name and the ability to speak So even though we're gonna wind up creating dogs and cats and cows and and horses and sheep all of those animals need to have a name and The ability to speak. Alright, so if you find yourself starting to create a number of objects that have similar features That's the situation that we're in we're trying to create animals And we want all those animals to have a name and a speak method Then creating a generic object to contain all the shared functionality. That's what we did we created a What they say a generic object type to contain all the shared functionality. That's what we did. We created an animal class where they all wind up with a name and the ability to speak. Right? With a name and the ability to speak. And inheriting those features into more specialized object types can be convenient and useful. Inheriting those features into a more specialized object type. What would our more specialized object type be? Yeah, dog, cat, cow, horse, sheep, those would be our more specialized objects. So when we look at this code, we see that we can use all of the, hey, thank you for the raid.
Coming through with, with the heat today, you got the whole crew today. Hey, what's going on? Thank you for the raid. What are y'all up to? What's going on Raiders? Thanks for stopping by. We are in the middle. We are in the middle of trying to make object-oriented programming make sense. We just covered encapsulation. We just covered abstraction. And now we're getting into, I'll give you a little, I'll give you a little heads up. Don't tell the other folks that have been here for a while. We're getting into inheritance. So stay with us. Stay with us.
We're getting to inheritance. Hope your stream went. Well, I hope you're doing well Thank you for the raid. We got it. We got to get the this coming back to you Hey, I appreciate that. Thank you Let's uh, I was a let's talk we got it. We got to figure out how to get this swinging back your way We got to get it. We got to send a raid your way. He's giving us all the love We got to send some love your way to thank you. Thank you for the raid All right, so we're reading this like really convoluted thing that the MDN gave us. If you're find yourself starting to create a number of objects that have similar features, we're trying to build a farm, right? We're trying to build a farm. And so we're gonna have a lot of animals on the farm. So we find ourselves starting to create a number of objects that have similar features. And what we created was an animal class where every animal has a name and the ability to speak.
Right? Name and the ability to speak. And we wanna start being able to extend this class so that we can start adding the stuff that dogs would need, start adding the stuff that cats would need, horses would need, et cetera. and what we're noticing here is when we look at the dog class we don't actually do the this dot name anymore we're not doing this dot name to add a name to the objects that come out of the dog class we have this beautiful super command and And this super command enables us to take all the stuff that came from the parent. Now you might call it a class and a subclass, but tonight we're gonna call it a parent and a child. We're used to those kind of relationships so this far, so I'll let the nerd shake violently, but we're gonna call it a parent and a child just to make it a little bit easier because we're talking about inheritance, right? Just to make it a little bit easier for ourselves, the code we need can run off, that's okay. All right, so we have our parent class, which is animal, and our child class, which is dog. Nerds everywhere quaking, right? And we have this lovely super, which enables us to grab all the stuff that was on animal and use it inside of dog. Why is this so helpful? Why is this game changing? What does this enable us to do? chat, what are some thoughts? Woo wee, it's dry.
It is dry AF, exactly. It's dry. It could enable us to not have to repeat a lot of code. We can get rid of a lot, a lot of duplicate code and we can go from what would be, as we start to build out this animal class, maybe lots of line of codes to one singular line when we wanna use it inside of the dog, right? And so dog can get all of the stuff that we had from animal, but it can also start to extend it and have just stuff that the dog needs like a breed, right? So we start to get this little bit of a little bit of a fear where we can get all of our, let's go back to the MDN here. We can start realizing, hey, we're creating a number of objects that have similar features. That's why we create the animal class. We need all of our animals to have a name, the ability to speak. And then we created that generic object, right? That contains all that shared functionality. And then we can inherit those features into more specialized objects. We have just inherited all of the stuff from Animal into our Dog class. And this enables us to start keeping our code organized, but also reducing the duplication of our code, right? So now we can run off and build everything that we need for Dogs.
We can build everything that we need for cats, but we're also not redoing, giving them names. We're not redoing them, giving them a name to speak, right? We can start to specialize our objects and inherit all of that stuff. So it stops us from having to repeat so much code over and over and over again. Cool. Would super only inherit the past property name in this case? Yes, in this case, it's only gonna take name. Now, there's gonna be a lot of stuff that we see tonight with these kind of classes and literally your homework. The thing that I want for you to do most for homework is to play with this code, break it, try doing different stuff, try building more and more children with these classes. If we build a dog, you build a cat, right? That's gonna be part of your homework. Anything that you might have that's a little question, I want you to play with it, formulate questions, and when we come back Thursday and we're reviewing all this stuff, we'll review and we'll answer a lot more questions then. Cool. All right. So by using these classes, both the parent and the child, we just eliminated a whole bunch of redundant code, a whole bunch of redundant code.
And what I wanna do is I actually wanna see this in action. I wanna see this in action. I'm just gonna go ahead and copy this and I'm gonna open up my inspector here. We're just gonna do it in the inspector. We're gonna do it on the fly. I'm just gonna paste this in. Beautiful. So what we did here is we have our animal class and we extended the animal class with the dog class, right? We have our parent animal and our child dog and we created a new object, or a new dog object, and we passed in two things. We passed in the name of Simba, and the breed of Shepherd, right? And so, if we look at this real quick, Simba gets passed in a name. Where is name then passed to? Where is name then passed to? Yeah, super, super, right? We're gonna take that name and we're gonna pass it into the parent, right?
Super, we went up a level, right? We went up a level, we passed it into name. So now we're gonna grab this.name, which in this case will be Simba. So the object that comes out of this dog class will have a name property, but that name came all the way from the parent class. Right? Then we passed in shepherd and that went to breed. We can see that we didn't pass that to super. We kind of just skipped that line. And this.breed is now shepherd. So we wound up with the name from the parent class and the breed on our child class. And if we ask for Simba, we can see that we have a dog object that has a name of Simba and a breed of shepherd. And if I open it up, what else am I going to expect to see? We keep digging. We can see that there's a speak as well. We see that there's a speak as well.
So if we go ahead and we do Simba.name, we get Simba. If we do Simba.breed, we get shepherd. If we do Simba.speak, and we use our parentheses because it's a method, we get Simba makes a sound, right? So we can inherit all of those properties, all of those methods from the parent, right? We get all of it. And I know right now it seems simple, right? Right now there's only there's only one property on the on the parent constructor There's only one method on the on the parent constructor But imagine if we were building out like real animals like we needed to have a lot of stuff Like the name of the farm their actual name their farm name we needed to have number of like number of legs all this stuff that we could think about that all of our animals would need and And then whenever we created a new dog class or an extension of that class, we got all the stuff that comes from the parent without having to retype it out. Without having to retype it out, well, we're gonna eliminate a whole bunch of redundant code. Got your warning, folks. Normally, a whole bunch of redundant code. Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go.
Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go. Let's go, Inheritance baby, let's go. Oh Boy All right, inheritance, baby. All right, we can make a class from another class for our hierarchy of classes that share a set of properties and methods. What? Let's read that back. We can make a class from another class for our hierarchy of classes that share a set of properties and methods. Right, like we can start to bring a lot of organization to our code with this inheritance, we can start to get out a lot of redundancy of our code. We can get it out by using this inheritance, right? We can start to really think through how we could build real stuff. So if we're thinking about our animals, right? We now have the ability to have a parent of animal and a child of dog, of cat, of horse, of cow, right? But what if we wanted to also have a bunch of other animals? What if we wanted to have like domesticated animals and like wild animals, right?
Like animals that we put like selective pressure on, right? Right, that might have like breeds, like dog has a breed, right? What if we want to separate dogs that we put like, that are like from husbandry, right? from like, I don't know, like a macaw that we caught in the wild, right? Like, don't catch macaws in the wild. I'm just throwing that there as like a zebra. Talking about a zebra. Exactly. A zebra, right? Birds aren't real. Like a zebra, right? We could start to really think about how we might build that out, right? All of our domesticated animals would have a breed, right? I think that's true. I don't know.
They would all have breeds, right? But maybe our wild animals might not actually have a breed so we could have a parent of an animal class and then what could our subclasses be what could our subclasses be yeah we could have like a domesticated subclass and a wild subclass right or like a a domesticated child and a wild child wild child right And the domesticated child would have breed. It would have a year since domestication, right? Like we'd have all this stuff that we would need for domesticated animals to have, but our wild child wouldn't have any of that stuff. It might have the location where they came from, right? Like where is their natural habitat, right? So our domesticated child might have breed, whereas our wild child might have natural habitat. And so we can start to build out our farm from a parent animal to domesticated and wild. And then in domesticated, we could extend to have our dogs, our cats, our cows, And we could extend our wild to have our zebra, our macaw, all that stuff. And so we can start building out this beautiful chain of inheritance where we don't have to constantly repeat ourselves whenever we want to add a new domesticated animal, right? Whenever we added a new domesticated animal, not only could it inherit the breed from the domesticated class, it could inherit the name and speak from the animal class. And whenever we have a wild child, a wild, a wild object, like a like a zebra, it could inherit where it was originally its natural habitat, and then also get the name and the speak from the animal. And so just as this layer of organization to our code, it adds this ability to have this inheritance, so we don't have to repeat ourselves every single time we start to add new stuff. This is kind of cool. I think it's kind of cool too.
I think this is where it starts to get neat. It really is like a point where we can start to really think about the intentions behind the code we're building. We can start to get this reusability. We can start to get this abstraction, this encapsulation. These are real things that I want you to understand that you can use. Once you understand, these are things you can put it in the practice that will actually save you time. Learn all, yeah, yeah. Cool. This is starting to get very neat. My brain doesn't hurt anymore. Oh, we got, we got some, we got plenty of time left. We got some plenty of time left for the brain to hurt. All right. Let's go ahead and what I want you to do is I want you to make a new child class. We made a dog class.
I want you to make another child class. I want you to make your own child class. Pick a new animal to add to the farm and try and extend our animal class. So in the starter code for today, I actually have a completed class already here. I have dog extension of cat extensions. I have a bunch of stuff that's already in here. So you can always peek at this. You can peek at the slides because that's a little bit different. Some stuff that we still have to get to. So you can kind of look at the slides if you need to, but I want you to go ahead and just practice extending the animal class. So type out the animal class, type out the extension. We're gonna put five minutes on the clock. I really want you to get your hands to get on this keyboard. I know we've been doing a lot of like theoretical talking nonsense. Let's get down, right?
Get your fingers on the keyboard. You don't have the materials for today. You can get the materials on our discord Just do exclamation point discord agreed to the rules and all the materials you need for today are in the follow along materials channel All right. I'm gonna put five minutes on the clock. You can go ahead and open up the objects The objects child class. I don't think you need to create your own. Just just go ahead and extend the animal class All right, five minutes on the clock. I really want you to think through this. I want you to add some stuff I want you to try it, break it, mold it, bend it. Take five minutes and then we're gonna go over it together. You got this. Funk, we're gonna get over that in a little bit. We're gonna go over that in a little bit. If you're looking at that completed section, there's stuff in there we haven't gotten to yet. Hitting not really.
We're going to see how to get that same The same stuff we would got by using let before i'm going to show you how to use how to do that today uh with private stuff We use that word loosely When's the next story class, uh, not this week, but probably next week Got this folks, 3 minutes 30 seconds left. Can you overwrite a function? Yes, you can. We're actually going to see that in a little bit. Remember about how this is all working underneath the hood. Remember, we're using classes, but underneath the hood, it's all prototypal inheritance. And since it's all prototypal inheritance underneath the hood, that means the objects always check themselves and then go up the prototype chain. Keep that in mind. Ooh, Pizzy, hope they're okay. Can we re-extend? You can extend an extension, yeah. Is there a way to privatize the properties method? Yeah, we're gonna talk about that in a bit Why is it called super because they watch too much um comic book heroes Question my comics they read too many comics and watched too many superhero movies Made a dragon nice yeah glitch that super means above in Latin or something there you I'm doing hippopotamus. I love it All right, folks, come on back. Come on back.
Let's take a look at this together. All right, so let's take a look. I'm gonna grab the animal class and I'm gonna plug it in here. All right, we had our class animal and we had our class dog. I'm just gonna copy this too, make it easier. There we go, so we had our class animal, we had our class dog. Let's do another extension here. So let's go ahead and do class, let's say cat, because we talked about cat so much. Let's do cat extends animal. Now, when I do the extends animal, this is where I get a lot of access to the original or the parent class, right? I get access to all the methods. And somebody asked, wait a minute, can I override the speak? Like, can I have a speak on my cat method? And you can. So let's go ahead and do this.
Let's go and build our constructor. Let's take in just kind of like dog, which feels kind of dirty, name and breed, because we're repeating ourselves, which means that there probably should be some like class in between animal and the Dog and cat but let's just go with it for now. We're gonna do super of name and we're gonna do this dot breed equals breed and we can come down here and Do a speak Right, we can come down here and do a speak and if we do a speak Let's just console log meow for now somebody asked wait Leon can you can you override the speak that was in the original animal even if it is extended from animal and the answer is yes because all these classes are just what Classes are just what? Syntactical sugar, baby. It's that sweet, sweet sugar underneath the hood is still prototypal inheritance. And what we know about prototypes and prototypal inheritance is that objects always do what before they wreck themselves? Objects always do what before they wreck themselves? They always check themselves before they wreck themselves, right? So no matter what object comes out of cat, it's always gonna look at itself first before it goes up the prototype chain to get something else, right? So there's still prototypal inheritance underneath the hood, which is important to remember. Cool, so we have our lovely extension of animal. We have our constructor and we're using super to be able to get access to the properties that are set on the constructor, right? We're gonna be able to pass in a name and be able to get this name set on the object that comes out of this class, right? We also have the ability since it's an extension to have access to this original speak, right? If we wanted to, we could use super to get this speak again, but we'll see that in a second.
And we're able to also set methods on the new thing that we created. Right. And so this extension gives us the ability to set new properties, grab old properties, set new methods, and if they're not set here, we can always go with the prototype chain to get the other ones. Cool. So, this idea of having classes that are parent and children enables us to reduce the amount of redundant code. It can speed up your process, but more importantly, it can give you a way of thinking about your programs in a way that starts to combine all this stuff. We're using encapsulation to fuse our data and our functionality. We're using abstraction so we can just work on singular pieces at one time. We're using this inheritance so we don't have to keep reinventing the wheel each time we want to use something. If we go back to our original example of the gaza classic pro. Right, we probably have something that's like a an espresso machine class and then we have some subclasses that give us access to the water boiler or the steam wand or some other bits and things, right? Gonna be able to start adding structure. And like I said, we don't want to have to we don't I don't want you to freak Out about understanding yet. I have to show you all the basics and we start to use it Once we start to use it, then we can worry about understanding and once we worry about understanding we can worry about building Don't try and skip these steps is the reason I showed you that in the beginning is let me show you this Let's use it a little bit and then the understanding will come as we start to use it. I know this is a lot I know these are some meaty topics, and especially if you didn't do the reading or you didn't get through the chance to see this at a first pass, it could be a lot.
So let's use it, let's try and understand the basics, and then we can start building with it, cool. All right, we talked about inheritance. We know that inheritance makes a class from another class for our hierarchy of classes that share a set of properties and methods. And at the end of the day, it's all you remember is that enables us to share a set of properties and methods without having to rewrite all those methods and properties, you're doing good. Cool. Made the child class together. Let's keep pushing. Let's go back to the farm. Let's go back to the farm and introduce something and then we're gonna take a break. You know what? Let's actually take our break a little bit early, right? We've covered a lot so far. We've went through encapsulation. We went through abstraction. We've done the beginnings of inheritance.
We saw how we can have a parent class with properties and methods and inherit those properties and methods into a child class without having to rewrite all that code. Right? Let's go ahead. Let's take a break. Let's let our brains meld on that a little bit. And when we come back, we're going back to the farm. We got some other stuff to talk about. All right, folks. If you're new around here, we like to be healthy. We like to give our minds a little bit of a break. We like to look out the window, let our eyes focus on something farther away. We'll see you in five minutes. You You You You You You know why they keep running Papa John's ads? you know why cuz you hooligans keep buying Papa John's but I keep advertising pan wait until that data leaks wait until the Papa John's data leaks I got y'all fans are gonna come swift swift if you don't think I would download the leak compare it to all y'all emails and ban y'all. Y'all know who I am.
People sweating right now. All right, folks, come on back. All righty folks, let's go back to the farm. It took our break, let's go back to the farm. All right, we have a problem. Whenever new workers join the farm, they keep renaming the animals in our system. and it's starting to get a little annoying. We're trying to build this program, we're trying to have all of our animals have a name, and our new workers just keep renaming them in our system, and it's getting a little annoying. So here's what that looks like. We have our lovely animal constructor, sorry, our animal class that gives it a name property and the ability to speak. And we can go ahead and create that new animal. And we gave the name of Simba. So when we do name.Simba, it equals Simba. However, somebody can come in and rename it to Bob. So let's take a look at this.
We can have our lovely animal class here. Let's use the inspect tool. Let's clear it. And we have our lovely animal class here. We can go ahead and create Simba. So we can do let Simba equal. Can let Simba equal, and we'll say a new animal. And inside that animal, we're gonna pass in the name of Simba, right? Great, now if we ask for Simba, we can see that it is an object that came out of the animal class. And when we passed in Simba, it went into the constructor and it got a name property of Simba. That's why we see name Simba. However, we can use Simba.name not only as a getter, but we could also do Simba.name and set it equal to Bob and it's a setter. Cause now when I look for Simba back, we can see that the name has been updated to Bob. So all these new employees are coming in, even though we're creating the animals from our class, somebody can just come in and change the name of our animals. and it's you know I'm getting a little I'm getting a little I'm getting a little pissed off right they're coming to our farm they're new not putting any respect on their names changing them from Simba to Bob and I've had my I've had enough the audacity how dare they exactly so so what could help here what What are some things we could do?
Well, let me show you. Here we're gonna use a little thing called a getter. And we're gonna combine this with a convention. So we're gonna use a little bit of something new that comes that we can use with these classes. And we're gonna use a convention as well. So here we go, let's look at this code here. We can see two things. we can see here that our constructor is no longer setting just name. The constructor is setting underscore name, right? And it's still all work. So let's go ahead and plug this all in. I'm gonna plug this into the console real quick, just so we can see it. Don't worry, I'm gonna make it, we'll come back. I just wanna see it. Plug this in here, boom.
We can do Simba and when we ask for name We get name back right we can ask for name we get name back, but when I do Simba name equals Bob Right and I asked for Simba back. I Still have Simba in there Right even though I tried Updating name nothing happened. When I did, when I asked for Simba back, like if we do Simba.name, I still get Simba back. What type of sorcery is this? All right, let's do this one more time. Let's do this one more time, because I just want to make sure the folks in the back saw it. So our constructor's a little bit different, our class is a little bit different. We have Simba. If we do Simba.name, we get Simba back. If we try changing the name using dot notation to be Bob, it doesn't do anything. When I ask for Simba.name again, I still get Simba. All right. So now the riffraff that are coming to our farm can't rename our animals. And let's take a look at this. All right.
Let's take a look. Ryan, do exclamation point 100 devs and you'll figure everything that you need. All right, so if we look, we can see that we're not actually setting a name property here. We're setting an underscore name, right? So that's something to keep in mind here. We're setting an underscore name and we have this lovely get with the name. And when we say get name, look what it's returning. It is returning this dot underscore name. And so we now have this getter that is using the underscore name that we set in the constructor. Yet we can still just call it name, right? We can still use name. And when we do name, what we're actually doing, when we use name here, we're using the getter. And the getter goes, right, the getter goes and gets underscore name. Right, gets underscore name. And so when we are actually trying to do this.name, the only thing that we're doing, Whenever we type in this dot, whenever we do Simba.name, the only thing we are using is this getter.
Does this getter enable us to update the name property? Is there any way that this will ever update underscore name? No, there's just no way. there's no way for it to go ahead and update name. Yet we can still use it to our advantage, right? We're starting to see this idea of, we're gonna use a word here that's gonna make the nerds quake a little bit. We're starting to see this idea of like private properties, right, private properties. And when I say private, I just mean that. They're kind of abstracted in a way, right? We have the ability to get them. We don't have the ability to set them. Right, and so we can set them when we create the object, but once the object is created, there is no way to change the name property, so this solves our problem of new folks renaming our animals, but it also enables us to sleep a little better at night. Right? It enables us to sleep a little bit better at night because we know that some other engineer is not going to come along and accidentally rename our animals. But let's take a look at, let's take a look what's happening underneath the hood here.
Take a look what's happening underneath the hood here because we're going to see something that's a little, A little a little funky a little funky. So let's go ahead. Let's copy this Let's open up the inspector Let's go to our console. Let's place this in and let's look at Simba if we look at Simba. Oh Wait a minute Wait a minute What's the property that Simba has It's underscore name. Wait a minute. So there's not even a name property. It's underscore name. Wait a minute. Interesting. So does that mean I could come along and do something like this? And now if I ask for Simba, oh, now it's Bob. So wait a minute, huh? I thought this whole point of having the getter was so that this doesn't happen. Well, that's the underscore.
The underscore does nothing for us. There's nothing special. Think back to our constructors. Our capitalizing of the constructor did nothing special, right? This has nothing to do with security. It has nothing to do with like making this so that nobody can really do it. It's a convention that we are all agreeing to. whenever you see that underscore what should be screw what am I screaming at you whenever you see that underscore what am I screaming to you I'm screaming don't change this don't touch this back up Don't touch it. Now, can some engineer on the team be a jerk and not agree to this convention? Yes, but by me using the underscore, all of us that are here right now, we can agree to not touching this stuff, right? To leave it alone, right? to never, ever update or change a property that uses that underscore. Just like we made capital letters of our constructors, right, it's on site. We outside, you touch that underscore property, we outside, that's all I'm gonna say. We outside, and I'm gonna win.
So if we take back and look at this, we can see that, oh, well, we can see that we're actually setting underscore name as the property. And then instead of having to use this whenever we're working with our lovely object, we have this new getter that we could just use the name. We can just use name, right? Cause we never want to use underscores when we're actually trying to use dot notation to access things off of our objects. We should never do that, right, right? And so we can use name, which uses the getter, and underneath the hood, it goes and gets the name property for us. So we get this nice, convenient way of accessing the properties on our objects, but we don't have the ability for somewhere else in our code to fuck it all up. That makes sense. It's just a little bit of convention that can save us some time. A little bit of convention that can save us some misery, right? Let's run it back, let's take a look at it. Let's see what trouble we can get into. when we were using our original class, anyone could come along, right? And it's not like we're saying like anyone, like when I say anyone, I don't mean like random users on the internet. I mean, other engineers that are working on our team.
Could there be a scenario where somebody does this in the code, right? Could there be a scenario where somebody goes, oh, let me add a name to an object, right? Wait, could that happen? Yeah, somebody absolutely somewhere in our code base could go ahead and set the name to Bob. Should there ever be a scenario where someone does this? Should there ever be a scenario where someone does that? No. Anyone that's on our team would see that underscore and they would run. They would say, I should not touch this. I should not do this. I will mess up the code if I do. So to make it a little bit easier, since we know that we can do this, since we know we can just use regular name, we don't want somebody to be able to accidentally do this. We don't want somebody to accidentally be able to do like name equals Bob. And then next time we ask for simba.name, it's something different, right? We don't want that to be able to happen, right?
So to get around this, we introduce something new, which is when we are building our constructor, we don't actually use the name property. We're using underscore name. And then to make it simple for other developers and even ourself, we enable it to just use the regular name and we use a getter to say, hey, when somebody does the name property on an object that comes out of this class, what they really want is this underscore name, right? They really want this underscore name. And so the getter goes and gets the underscore name, but the person using it only ever sees name. They really have no idea what is happening under the hood Right, just like when we get in the car We have a steering wheel that enables us to like turn the car go about our day but do we ever have a button on our steering wheel that enables us to Drop all the oil out of our engine Would we ever put that on our interface, right? We ever put that on our interface, like drop all the oil. Like we're just trying to like change Bluetooth and like all the oil drops out of our engine as we're going down the highway. We would never ever put that button on our interface. Right? And so now we're starting to control the interface of this object, right? We're starting to be like, okay, this is what you get access to, right? This is what you get access to, but I'm going to make it so that you can't shoot yourself in the foot. I'm gonna make it so that you can't actually ever set the name to something else because the problem we were having is folks kept updating the properties on our objects. They kept changing the names of our animals.
And so by using this underscore convention and using getters, we can expose things to the interface. We can expose that you can use the name property, but I'm not going to give you the ability to fuck up the rest of the code. Right? Cool. Constantly constant underscore is something JS developers agreed to use in order to semantically represent a private property. It's a property that is intended to only be accessed truly within the class. That is the mean to prevent cases where people attempt to change the property from outside the class. Winner winner, chicken dinner. Why is that so important? Why is it important to have these private properties that can't be changed outside of the class? Why is it important to have these private properties that can't be changed outside the class? Yeah. Yeah, we're starting to see how encapsulation, abstraction starts to fuse with these other pillars, right? It enables us, it's the mechanism by which we can do abstraction, right? We don't have to worry about changing the name on stuff anymore.
There is no way for us as an engineer working in this code to accidentally change the name. And right now it's just simple. We're just saying the name but as we start to get a little bit deeper in our code We're gonna start to see real meat behind our classes and they're gonna be scenarios Where we don't want to lose sleep at night that someone could accidentally change stuff. We want that stuff Abstract it Right. We want that water boiler abstract it. We want that engine abstract it We don't want anybody to be able to change to drop the oil out of the engine. We don't want anybody to be able to like remove the water boiler, right? We can abstract stuff out the way. And so this is a mechanism for that abstraction. But we still get all the benefits. We can still use it. We can still interact with it. It's still on our interface, but we don't have to worry about it at the end of the day. All right. So when we see the underscore, what does that mean?
When we see the underscore, it means what? Don't do what to it. Private, runaway, no touchy, don't touch it, don't mess with it outside of that class. Cool. All right, so we're introducing a new scenario. We just rescued a bunch of animals like a bunch of animals. Jason Derulo, hey, thank you for the gift of subs. Hope you're doing well, thank you for being here. All right, we just rescued a bunch of animals, all right? How should we build out our system? Let's think through this. Let's build out our system. So we have in the starter code for today, we have this completed code already. So I'm gonna come to this completed code and I'm just gonna comment it all out. I'm gonna comment it all out.
And we're gonna do some of it together. Actually, I'm just gonna do the whole thing. Cool. I'm gonna type it out so we can see it kind of like line by line, how we're gonna do it. So we wanna start adding a bunch of animals. We're gonna have all these new animals coming in and we wanna be able to build this out that we can handle all of these new animals that are coming in. We're gonna have dogs, we're gonna have cats. We're gonna have to handle all of these different things that are happening with these dogs and these cats and we're gonna see some cool stuff here. All right, so let's build out our, I'm gonna give myself some room here. All right, let's build out our initial parent class. So class, animal, and we're gonna go ahead and put in our constructor. Cool. And we know that we want our animals to all have a name. Should we code along? No, don't code along with this.
Let's just talk through it. So we know we want all of our animals to have a name. So in the constructor, we need to bring in a name as a parameter. And we know that when we go to set the name property, what should it be? It's no longer just going to be this.name. What should it be if we want all of our animals to have a name property? Yeah, I want it to be underscore name. That way, we can abstract out the name and not have to worry about it. And that's going to be name. Whatever gets passed into this constructor for name is now set to a property called this underscore name. Now, since we're using the underscore name and it's kind of private, we know not to touch it outside the class, what do we need so that we can actually use the name in our program? We need a getter, exactly. We're gonna do our get name and that's gonna be our getter. So now we can use name anywhere. and when we do the get name, we wanna return this.underscore name.
This is what's gonna enable us to use the name property, but when we're doing it, we're really getting this.name. And that way, there's no way for our user to actually use name to update the objects. The only thing that name does on any object that comes out of this class is return the underscore name properties. Cool. All right, next we wanna add a speak. Wanna add a speak method. Cool, so we have the speak method and we just wanna do the original that we did. I think it was just console log makes a sound. So let's go ahead and console log. Let's do our ticks. We're going to say, um, we can say inside of our template literal here, we can say this, that underscore name makes a sound. There we go. Why don't you need the parentheses when calling name? Because the getter is handling that for us. the get's handling that for us.
Like I said, we gotta see it, we gotta use it. We'll have plenty of time to go into the nitty gritty details as we go on. All right. So we have the ability to now have an animal that has a name, underscore name property. We are able to access that underscore name property by having a getter that gets the name. And they all have a speak method where it grabs the name of the animal and says it makes a sound. All right. I want to have as animals on the farm, dogs and cats, right? All right, I wanna have dogs and cats. All right, I wanna have dogs and cats. What could I do so that I could have all objects that spit out dogs and a bunch of objects that spit out cats? What could I do? We could extend, exactly. I saw somebody said, why does this underscore name still work inside this method? because we are still within the class.
Remember, this underscore is just so that we can use it inside the class, but anywhere outside the class, it really like just doesn't exist, right? Cool. Somebody said, why can't we just have like name as a method? Well, then we wouldn't be able to use the name as a property, right? Wouldn't be able to use name as a property. The get enables us to use that name as a property. All right, so let's go ahead and create our dog class. We can just go ahead and say class dog extends and we can say animal. Beautiful. So now anything that comes out of this dog class will get all the benefits of the animal class. And so let's go ahead and build our constructor. We want to be able to pass in the name still, that we'll send up to the animal constructor with super, but I also want one property on my dog. I want one property on dog. Posture check. Hey, thank you for the posture check.
Jomie, thank you for the posture check and some kind of fire. Thank you for the hydration. Cheers to you. I'm going to use breed, I'm just going to use breed. Cool. We saw that already. All right. So in our constructor, we can use, if I want to pass name and I want to use the animal constructor, how can I pass name to the animal, the animal constructor? Yeah, we can use that super keyword and we can pass in name. And then if I want to set breed, I'm going to do this. But what am I going to do here with breed that's a little bit different this time? What am I going to do with breed that's a little bit different this time? Yeah, we're going to do the underscore just so that we know that nobody should mess with the outside. And so we'll say this underscore breed equals breed. Nice.
And now that we've used that underscore here, what do we know that we need as part of this class? What do we know we need as part of this class? We need a getter. We need a getter. So we can do get breed, and we can go ahead and say, return this dot underscore breed. So now we'll be able to get the breed out even though it's a private part of this class. All right. And I feel pretty good about dog. I want to show you something, I want to show you something else too. When we run the speak method, I want to add a speak on the dog as well. Just to show you kind of like how this can work. So inside my speak method, I want two, I want two things to happen. Inside the speak method, I want two things to happen. I want to see the original speak that comes from the animal class. And I want to be able to create my own new speak that also runs.
How do you think I might be able to get the speak that's on the animal class? Yeah. All right, I see some folks doing it. We're gonna use the super again. So we can just do super.speak, and that'll grab the speak from the parent class. Nice. We're still inside of a method, so I can also console.log inside of here as well. So I'm just gonna do a console.log. We can just use the name again if I guess. We can do... This.name, I guess a dog would bark, right? We're gonna say barks. Don't lose it yet. Don't hyper focus on this. Don't hyper, don't, just hold on, hold on.
Don't hyper focus on this. Just leave it, just leave it, just let it be, it's okay. Let it pass through you. If you felt a, if you felt aah, aah, that's your inner code weenie trying to come out, just let it be, let it be, it's okay, it's okay, it's okay. Let it marinate, we'll get there, we'll get there. All right. So, we have two things inside this method, which is pretty interesting. We should be able to grab the speak from the parent, and we're also gonna see this console log of barks. So when this speak runs, we should actually, we should see two things. We should see the name makes a sound, and then we should see barks. I just wanted to show it so we can see like the super happen. I just wanna be able to show you that like, we can grab the methods from the parent, okay? Okay, let's make one more. Let's make a cat, a cat class. Class cat extends animal.
And we're gonna build out our constructor. Our instructor is going to have a, let's say, probably like a name and a breed again, right? Oh, this feels bad. Let's grab the name from the parent using super. So that'll go up to the animal class. And let's go ahead and set breed again. Cool. And since we're using that underscore, we had to get a getter. And we'll say get breed. And that's going to be return this.underscore breed. Right? And then we're going to do a speak. And let's do the same thing where we get the super as well. So we'll do super.speak. And we'll also console.log something new.
And where we'll just kind of use the name, this.name and meows. And this feels horrible. Why? Why does this feel horrible? Because I'm rewriting a lot of stuff. If I was to think about this, if I knew that I was only going to have domesticated animals on this farm, like if I knew I was only going to have, I don't know, do wild animals have a breed? Does anyone know this? Or is it just like animals that we put like selective pressure on? Do you like wild animals have breeds? I don't think so, right? They have species, but like not breeds, right? Do they? Are there like different breeds of hippopotamuses? Yeah, no, there's not right. So it's just it's just animals that we put like like that we've domesticated or put like selective pressure on right that have breeds.
I don't know. I mean this whole lecture. I don't know. So I'm starting to feel that there might need to be something happen, right? There might be something in my code that I need to think through. I might want to put breed in my animal class, right? I might need to put breed in my animal class or if I knew that I was going to have both domesticated animals and Wild animals that I probably need to extend animal into wild and domestic and then have dog and cats extend from from domesticated and Hippopotamuses and zebras extend from the wild class Saturnalia said breed a specific population that is selectively bred for the preservation of specific characteristics, whereas the species is the largest group that can produce fertile offspring through breeding. So yeah, so wild animals do not have breeds, right? So something we could do is instead of extending from animal, there could exist a world where dog extended from domesticated. I can't spell right. Domesticated and zebra would extend from wild and we could put breed on the domesticated class and we could put, um, we could put like original habitat in the wild and then we wouldn't have to redo this breed nonsense for both dog and cat. Right. So that's something to think through right something to think through like as we start to add more layers to this Right, we could start to think like wait a minute, huh? If I'm repeating myself Maybe I should redo how I am doing this hierarchy Right. I should maybe think about how I can redo This inheritance, right?
I want some stuff to inherit from animals But it seems like I might have a situation where I went to other classes that my animals inherited from one That's domesticated and they get breed and one that's wild where they would get species and then I wouldn't have to redo this breed multiple times right it would already be on the Domesticated class and I would be extending from domesticated and not animal Hmm So this would be a form of refactoring. Yeah, it could be a form of refactoring, exactly. It would be a form of refactoring that could help us do that, but it's just something to think through. It's more of a thought as we start to see how these classes can be used and come together. But I feel good with this. I feel like we have our animal class. I feel like we have our dog extension. We have our cat extension. And I wanna be able to use these extensions. So, let's go ahead and I'm just gonna copy all this. I'm gonna comment out the stuff we just did. Just in case I had like any weird little errors, I'm just gonna uncomment the one I did before. Beautiful. And I'm just gonna grab this stuff and I'm gonna go to the console with it. So, let's go ahead and open up our inspector.
And I'm going to go to console and plop all this stuff in and hit enter. All right, so if we look, what we did here is we put in our animal class. We put in our two children class of dog that extends animal and cat, which extends animal. Beautiful. And we went ahead and created some new objects, right? We went ahead and created some new objects. We created Simba, which is a new dog object. we passed in the name of Simba and the breed of Shepard, right, because remember, when we pass in Simba, Simba's gonna go into the name property of dog, which is gonna be supered up to animal, so that the ability to give it a name, the ability to give it name comes from this animal constructor. All right, cool. We saw that, we passed in the name, and we also passed in a breed. And we can see here on dog, we passed in a breed, and we set a underscore breed on the object. And now to get the breed, we would need to use the getter. Alright. Let's see. So we created a new dog, Simba and Shepard.
Another dog, Moshi, aka the Machine, and put in the breed of Pitbull. And then we also created a new cat, Salem, with the breed of American Shorthair. Cool. Let's just check these out and see what we got, let's look at Simba. All right, we can see that Simba is a dog, a dog object or an object made from the dog class. It has a name of Simba and a breed of shepherd. But we can see that both of these are, you see that both of these have the underscore and are both private, right? They're both private. So that means we should never do like Simba dot underscore name or Simba underscore breed If we want to see the name or the breed We can just do breed and The getter goes and gets the breed which is shepherd All right goes and gets the breed Which is shepherd? Well That's neat. If we want to try and mess with it though, and change it to something else, it doesn't work. When we ask for Simba, we can see that the breed is still Shepherd. Cool. So we have this lovely new dog called Simba, lovely dog called Moshi, and a new cat called Salem. all using our original animal class and the children class of dog and cat.
Cool. Let's actually see if we can like make them say the different things. Let's do Simba.speak and we can see that Simba makes a sound and Simba barks. Where did Simba makes a sound come from? Yeah, it comes from the super, right? We were able to get that, that Simba makes a sound from the animal class. If we look at the animal class, It had a speak and we went and got that speak. We're inside the dog, right? We went and got the super.speak. So we got the speak from the animal class. And then we had our own console log in our speak method. So this is big brain right here. When I did simba.speak, what actually ran? Let's get big brain for a second here. When I did Simba.speak, what happened here?
What method fired? Yeah, itself. The speak method on itself fired. It checked itself first and guess what? It had a speak method. And that speak method, the first thing it did was use the speak from the animal class. It did super dot speak. So it checked itself. It had a speak method. It just so happened that its speak method used the other speak method and then did its own console log. Right. If we look at that, We can see that, hey, go and get the speak method from the animal class and then do your own thing. So it checked itself first and then went up the prototype chain using super to get the other speak. Speakception. Yeah, like I said, today it's about using and getting, getting a glimpse of, to the understanding.
Right. And so we're starting to see all these things come together and then we've got to start using them. We've got to understand them. We've got to build with them. Tokyo Titan. And what video is going to watch for absolute beginners do exclamation point 100 devs here in chat. You'll get everything you need to know. all of our original videos are on YouTube. All right. Cool. So we now have the ability for Simba to speak, Simba makes a sound, Simba barks. We can also do Salem.speak, and we can see Salem makes a sound, Salem meows. Beautiful. So now we have the ability to have these different objects. Like, can we just take a second?
This shit gets me so excited. I really do get excited when this stuff starts to happen. Right, like we now have the ability to create objects. Those objects can inherit a bunch of properties and methods from a parent class. We can understand the reason why we're using objects in the first place is so that our data and functionality is all in one place, right? That's encapsulation. We understand that we can abstract stuff out the way so that when other engineers are working on our code, they're not accidentally renaming our animals. They're not touching bits of the code that they shouldn't. We've actually set it up so they can only actually consume. There's no way for them to update the name. There's no way for them to update the We've abstracted that out there without the way. And now we're using inheritance to stop ourselves from having to constantly repeat the same lines of code. We don't have to give all of our dogs, our cats, our cows, our horses, our chimpanzees, our hippopotamuses, the ability to have a name. They just inherit that from the animal class. And yeah, it's really simple right now, but eventually if we were really building out these robust classes, right?
We were really building out these robust classes. We would have the ability to have all this stuff inherited down into our cats, down into our dogs, down into our hippos, down into our horses, down into our cows, down into our pigs. And we wouldn't have to repeat all this code. And better yet, all that code, the important bits are abstracted out the way so that as new engineers come on board, They don't have to worry that they're fucking up your code What So when when when when folks talk about using object-oriented programming They go off this deep end of looking up all these really weird oddly phrased stuff that never makes sense, but I'm hoping that you start to see that, oh, encapsulation means that we can keep our data and functionality in one place. We don't have to worry about, is a variable declared a hundred lines down? With abstraction, we can make it so that folks can use the things that we've coded, but not fuck them up, right? They, they, they can't, they literally can't rename stuff. They literally can't give a new breed to something. They can only consume it. So that way when they're building and they decided to go build something else on the farm, they're not losing sleep at night going, Oh man, did I mess up Leon's code about the naming and the breed stuff? No, they can just build what they want to build and not worry about the stuff that we've abstracted away. And then we see the power of inheritance, right? We see the power of inheritance to where we can set something on a parent class and all of our children have access to it. That's really powerful. Like fuck weird definitions, like that's some powerful meaty stuff that we can start to use.
And like we said, we're gonna use it first. We're seeing it here, we're using it here. We're just skimming the top of understanding you're gonna come back on Thursday, and we're gonna review all this stuff, right? We're gonna review all that stuff, but we can start to understand why All right, so I just get really excited I don't know I like we're not like when this stuff starts to click I get I get hype all right We got a little bit more before we end up class tonight. We'll do a raid of course. I just want to see a little bit more Pussy, give me, give me, I know your brains are on the cusp. Dig deep, give me just a little bit more. Let me just, let me give you a little more. We got 15 minutes, give me 15 minutes. I know your brain hurts. Give me 15 minutes. Let's push. Go where, go where you think you couldn't go before. Right, we've been working hard. I know our brains are, we're halfway through.
Our brains are are are are are lean mean learning machines at this point All right. Let's learn one more thing and then we'll end tonight All right, so we're on the farm And we want to do a morning roll call right And fortunately, dr. Doolittle works on this farm. Dr. Doolittle could talk to animals if you're not familiar All right, so we want to do a morning roll call We want to hear all the animals speak, right? We want to hear all the animals speak. And so if we look at the code that we had in the complete file, we saw this bit right here. We saw that we created all of our animals, we created dogs, we created cats, and then here we put all of our objects into a what? Put all of our objects into a what? Into an array. And then we loop through all of those objects and we would see Simba barks, Moshi barks, and Salem meows. meows. Bark sparks meows. Wait a minute. Wait a minute.
Hold on. Wait a minute. Wait a minute. How does this work? They're objects. Each object is different. A should be holding two dogs and one cat. Yet speak works on both dogs and cats. What? So hold on, hold on, hold on, hold on, hold on, hold on. We look at it, we have two dogs and a cat. They're all in this array. we loop through the array, A would be Simba, then it's gonna be Moshi, then it's gonna be Salem. And we're calling speak on Simba, then we're calling speak on Moshi, and we're calling speak on Salem. Simba barks, Moshi barks, and Salem meows.
Speak doesn't care if it's a dog or a cat. cat, it just worked. Huh? Hmm. Let's read some definitions. So if you just read the books or God forbid, just watch the YouTube video. Cause I can't tell you how many books I read, how many YouTube videos I watched, how much documentation I read about this next concept didn't ever made any sense to me because here's exactly why it never made sense. These are copy and pasted from some very official sources that I love dearly, but hold on, stay with me. Code written to use an interface automatically knows how to work with any number of different objects that provide that interface. The fuck? Yeah, that's too late. You're using big words, I'm gonna treat them as disrespect. All right, hold on, let's read this again one second. All right, come on, I gotta get a little closer. Code written to use an interface automatically knows how to work with any number of different objects that provide the interface.
All right, hold on one more time. What the heck's an interface? Somebody tell me what an interface is. What's an interface? Oh yeah, we did this with APIs. It's just what we use to interact with something, right? Like if we were using our lovely Pokemon Pikachu pet here, my interface would be the buttons. I want it to interact with my Pikachu. Here's the buttons to interact with my Pikachu. So when I say interface, it's just a fancy word for saying how I interact with something. knows how to work with any number of different objects that provide the interface. Do my objects provide an interface? What's the interface for my objects? Yeah, it's the properties and methods. The properties and methods are like how I interact with the object, right?
Let's just stick with methods. The methods are how I interact with my object, right? So let's, so interface is the way we interact with something. The way I can interact with my objects are through its interface. And when I say interface, I just mean like the methods, like the methods are the way that I interface with my objects. All right. So code written to use an interface automatically knows how to work with any number of different objects that provide the interface. So let's go back and take a look. Here is my interface of speak. Does every single object that gets used here have a speak interface? Yeah, my dog has speak, my second dog has speak, and my cat of Salem also has speak. All right, so let's read that again. Code written to use an interface automatically knows how to work with any number of different objects that provide the interface. So all three of my objects, Simba, Moshi, and Salem, they all provided the interface of what? What was the interface that Simba, Moshi, and Salem provided?
Speak, speak was the interface they provided. So the code written to use an interface, this code was written to use the interface of speak. the code written to use an interface automatically knows how to work with any number of different objects, Simba, Moshi, and Salem that provide the interface. So my code automatically knows what to do because every single object provided that speak interface. Right? Simba did, Moshi did, and Salem did. Cool. Like literally, when you read about the thing we're building up to, these are the three paragraphs that they use to describe this nonsense. All right. Sibling descendants of a base class will all have the same interface, but varying implementations. What the hell is this? The fuck? Huh? Let's read that again. Sibling descendants of a base class will all have the same interface but varying implementations.
Mom came to pick me up, I'm scared. Oh fuck, I swear to I swear, I swear this is like when I was trying to learn what the heck. I'll just say it, polymorphism is these are these are like the real things they ask you. This is like this is the fucking this is like and this is the good definitions. This is like the good stuff. All right, hold on. Sibling descendants of a base class. Where have we seen sibling descendants of a base class? Dog and cat. Exactly. All right. Dog and cat are both children, are both children of our animal class, right? They both extend animal so that means dog and cat are sibling descendants. Don't go see it. We're in javascript world Good guess at the css, but we're in javascript land right now All right.
So sibling descendants dog and cat Of a base class in this case animal Will all have the same interface But varying implementations. What interface did my sibling descendants share? What interface did my sibling descendants share? Speak. They both shared the speak interface, but varying implementations. Okay. Dog and cat had varying implementations because dog did what and cat did what? Yeah. Dog barked and cat meowed. Dog, bark, cat, meow. So sibling descendants, AKA cat and dog, greatest television show of all time, cat, dog, by the way, of a base class animal, all had the same interface, which was the speak method, but varying implementations. Because if we look, we go back to the code. I should probably just stay over here and bring up my code. Because if we look, one barks, the other meows. Varying implementations.
If I was to create a new animal, let's say I was able to create a class of cow that extended animal and I forgot to give them a console log. would cal still have a speak method? Yes, even if I forgot this whole thing, the object would check itself for speak, and if it didn't have it, where would it go? If the cal we just created didn't have speak, where would it go? it would go up to the parent which is animal fuck yes yes you're doing it leon they're learning getting this shit one way or another you're gonna learn first try let's go this is the hardest fucking shit to learn and you're like yeah Leon And they didn't, if the object itself didn't have speak, it would go up to the parent class, which is animal, because this is just syntactical sugar. Underneath the hood is prototypal inheritance. We know objects check themselves before they wreck themselves and they don't have it. They go up the fucking prototype chain. Let's go. We don't get got. Let's go. Let's go. We outside, we online, let's go. All right, one more, one more. We're almost there, we're almost there.
Almost there, all right. When you're not sure of the objects type at runtime, let's just, let's get rid of and here. When you're not sure of the object's type at runtime, the most specific method is called. Therefore, the behavior of the method called may differ depending on the object's type at runtime. Hold on, when you're not sure the objects type, what type of objects have we seen so far? What type of objects have we seen so far? We saw cat and dog, right? We saw cat and dog. All right. So we saw cat, dog, and animal. So we're not sure if it's cat, dog, or animal at runtime. Like runtime just means like when we're like trying to use it. So if we're not sure if it's a cat, dog, or animal when we're trying to use it, this most specific method is called. Wait a minute, that's just what we just talked about. We just talked about that, right?
It checks itself, right? It checks itself before it goes up the prototype chain. This is easy. Easy clap. Let's go easy. Why do you have to talk like this? Why? Who writes this? I'm writing a book. Fuck it. I'm writing a book. This shit is, this is getting on my nerves. When you're not sure if it's a dog, car, animal, when you go to use it, the most specific method is called. AKA it's just going to check itself first. Therefore the behavior of the method called may differ.
No shit. Cause if it's a dog or a cat and they both have different speak methods, then it makes sense that their behavior, whether they meow or bark would be different depending on whether or not it's a cat or dog when you go to use it. Easy. Why do you got to write like this easy? Easy. Cool. Why is this code disgusting? Why Why is this code disgusting? This is bad code. Too many conditionals, right? Like, hold on a minute. In this case, we're checking to see if it's a dog. And if it's a dog, if our object is a dog, then we'll let it bark. If our object was a cat, we would let it meow, right? But if we were to build out a whole farm, we're talking hundreds of animals.
Each time I added a new animal, what would I have to update? I would have to update this disgusting conditional every single time, but we know, we know that we don't have to do this. We just saw that we don't have to do this disgusting conditional every single time. Imagine each animal we added, we would have to remember to come back and update this bit of code. That means our code is not truly what. If we would have to come back and remember to update this every single time we made a change, we know that our code is not truly what? Not dry, not dry. It's not abstracted. If I can't make changes in one part of my code and not worry about what happens somewhere else, then my code can never be truly abstracted. So I can't have this. This can't be a thing, right? This can't be a thing. If I was like making new animals, if I was making cows and chickens, and I had to also remember to come back to this point in code and fix it, this wouldn't work. It would never be able, I would never be able to have truly abstracted code where I could just focus on the thing that I was running. So, this works.
Do I have to update anything here? Do I have to update anything here? Like if I added a cow, a chicken, if I added a hippopotamus, do I have to come back and update this? Nah, nah. So what if there was a way to perform a single action in different forms? What if there was a way to have the ability to call the same method on different objects? What if I could call speak on dog? What if I could call speak on cat? What if I could call speak on chicken, on hippo, on cow, on pig, and not have to write conditionals and switch cases and remember to come and update them to never have a world without true abstraction. Well, guess what, baby? We don't live in that world. We live in a world where we have. How many more of this is baby? Let's go. Polymorphism baby polymorphism all right let's let's uh let's let's touch on the four pillars real quick to see if they actually make any sense all right Encapsulation, the process of storing our functions and properties into one thing.
The idea of fusing our data and functionality into one object. Why? Why do we care about encapsulation? What is the reason? What is fusing the data and functionality into one thing help us do? Well, it makes it easier for us to add new stuff. Our variables aren't all over the place. Our functions aren't all over the place, right? We don't have to worry about some stuff being a thousand lines down in the code base. We know that all the stuff we care about is in one location. So it makes it way easier to add new stuff when our data and functionality are encapsulated. It makes it easier to read through what was already coded. We know everything we care about is inside that one object. We make it so that you're not afraid to make changes. You know the functionality and the data are tied.
So if you change the data, you know your functionality is going to change. If you add new functionality, you know what data it's working with. Encapsulating it all into one object means that even if something else is happening outside side of that object, it's OK because your stuff is also abstracted. Starting to see how all these things come together, right? All these things come together. There are four pillars, but they really work in tandem with each other, right? Abstracting is hiding the details, only showing what is essential. But why? Why do we need abstraction? Extraction makes our code base into smaller, more manageable pieces of code, right? We can split the complexity of our project into many different manageable parts. And when we're working on these different parts, we don't have to worry about what's happening elsewhere in our program. This encapsulation and abstraction work together so that we can make changes and not lose sleep at night. We can update stuff. We can work on different parts of the project and know that we're not fucking somebody else's up work.
We're not changing their names. We're not changing their breeds. We are able to make new cows and not worry about if there is some messy conditional somewhere that's going to break when we add a cow, right? We have this inheritance. We have the ability for a class to share sets of properties and methods that we can extend from a parent, get all those properties and methods in the child and use them to keep our code. Why? To keep our code dry, to get rid of redundant code, to make it so that as we create new objects, we're not constantly reinventing the wheel, right? It makes it so that we keep our code bases smaller, tighter. And since all of our data and functionality is encapsulated, when we build these classes, we know all the bits that are working together. We can extend them if we need to grab stuff from the parent, right? And polymorphism works with all these things together so that the code that we write, right, uses an interface that can work with any of the objects that we created. So the interfaces that we give, the methods that we put on our objects, they should be able to work, that code that we write should be able to work with any of the objects that we created in our program. And why do we need polymorphism? It helps us avoid these nasty ifs, else's, and switch statements that we would have to be keeping track of, right? If we were to add a new cow, is that going to break something somewhere else?
No, we know that the speak method would work on any object that we create. And so we have this idea of polymorphism. Polymorphism bleeds into inheritance, it bleeds into abstraction, it bleeds into a capsulation. and all these things come together to create a paradigm where we can write code more efficiently, work together as engineers, make sure that when we are working together, we don't have to lose sleep at night. We can all do our own thing. We can all work collaboratively together, and we have a system that we can all agree to and follow. Cool. OOP, baby. Cool. Also, polymorphism makes your code more reusable. You don't have to worry about stuff breaking and it supports the other pillars. All right. Got some homework, folks. I hope you had some fun. I know, like we said, remember, we're gonna use it.
We're skimming the understanding. You're gonna come back Thursday. We're gonna have a lot of really fun stuff. Like Thursday, I got so much big news to share. We got so much fun stuff to share. We're gonna get our review in. Don't worry, we're gonna come see this again. I have some homework before you I have some homework between now and next class, please Please before you show up on Thursday Review play break the code. We went over tonight play with everything See what happens if you don't use super see what happens If you if you don't just used underscore name like play with it, you have to break it Remember we said we use we understand we build you have to come back and play and build with this stuff All right, folks, let's do a raid. Let's get you those channel points. Remember, you want those sweet, sweet channel points so you can get all these cool perks that happen here on stream. As always, when we raid somebody, please give them some love, give them some follows. Oh, Mastermind is on. We're gonna raid Mastermind. I'm so pumped.
Mastermind's amazing. If you have ever had any interest in DevOps, they are the man to make your dreams come true. Let's go over and say what's up to Mastermind. One of my favorite streamers on Twitch. Please, give them a follow, show them some love, give them the warm 100 devs hello. Alrighty, hope you had some fun. Hope you learned a little bit. Come back on Thursday, some amazing news to share, some amazing updates, some really cool stuff about the agency, and then also we're going to get some good, good review in. Alrighty, folks have a wonderful rest of your evening. Let's read. Issue tube.
End of Transcript