Classes & Objects | Java | Tutorial 26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about classes and objects in Java I'm really pumped to talk to you guys about this because when we're talking about classes and objects we're really getting into like the meat and potatoes of what Java can do and what makes it awesome so in Java we have all different types of data and there's a bunch of different ways that we can represent that data we can use things like strings we could use numbers like integers and doubles or we could use things like boolean's to represent true or false values but here's the thing in the real world there's a lot of things that can't be represented just too using like strings or numbers so imagine that I was for example like creating a piece of software that managed students at a school right if I wanted to just define a student's major so like a particular students major I could just create a variable I could say student major and I could set it equal to like whatever computer science or something right so if I wanted to just represent you know the students major it would be really easy I could do it using a string but how would I represent like an actual student you know we have the string datatype that we can use and we can give it you know a value of like computer science but there's no student data type like how could I represent a student inside of my java program and the way that we can do that is we can actually use something called a class and what a class allows you to do is it allows you to model a real world attribute inside of your program so essentially what we're doing is we're creating our own custom data type but it's a data type that represents like a real world entities so I could create a data type for like my phone and I could do that by creating a phone class I could create a data type for my credit card and I could do a by creating like a credit card class I could create a data type for a student and I could do it by creating a student class and when we create a class in Java we're modeling a real world entity were basically giving ourselves something that we can use to represent that inside of our programs so classes are really awesome and if that explanation doesn't fully make sense to you right now don't worry it's gonna make sense as we go through this video but for now just know that a class is basically a way that we can define our own custom data type because there's a lot of stuff that can't be represented in just like a string or a number so we can use classes to basically model a real-world entity so in this tutorial I want to create a student class so we're gonna create a class that represents a student inside of our program so I'm gonna come down here into my default package and if you're using Eclipse your setup should look similar to this but if you're not using Eclipse you just want to put this class in the same directory as you know whatever file you've been using with your main method so I'm just gonna right click on default package and I'm just gonna click new class and this will allow us to create a new class so down here we just need to give this a name so I'm just gonna call this student this would be our student class and generally in Java when you create a class you want it to start with a capital letter just like that so I'm gonna come down here and click this finish button and click that and eclipse is gonna go off and create a new student class for us and basically all this is is just a new Java file and inside here just says public class student so this is essentially how we can create a class in Java so let's talk about what this is and what it's doing remember we can use classes to model real world objects inside of our program and the way that we can do that is we can create this class like overall container and inside of here we want to give this some attributes so basically I can give this a series of variables so I can use things like strings or integers or boolean x' to define information about a student and basically we call those attributes so they're gonna be like the attributes of a student so over here I'm gonna start creating some attributes and let's think about what are the attributes that would define like a student in a college for example well we might want to say like first name and last name so we can create two strings one for first name and we'll create another one for last name and let's think what else can we define about a student well another thing would be GPA so we can say Jeep will make this a double and we could say GPA so I'll be like you know 2.5 3.5 whatever 4.0 if you're really smart we can also define their major so I can say string major and let's think what else we could define their age so I could say well I guess I could either define their birthday or their age just to keep it simple why don't we just define their age as an integer and what else what else can we define about a student let's define a boolean value we could say like on probation so this would basically be like true if they were on probation or false if they weren't so this is a boolean all right so what I've done is I created this student class and I've basically defined some attributes about a student so essentially what I'm doing is I'm saying that any student inside of our program can have a first name a last name a GPA a major an age and a on probation very low so essentially what we're doing is we're defining a student datatype and we're saying that a student inside of our program will have all of these attributes associated to it so what I can do now is I can use this basic student class and I can create individual students so you remember like if I wanted to create a variable in my program I have to actually like physically create the variable and give it a value right so if I wanted to create a number I'd have to be like int and you know whatever the number like the name and that I'd have to like give it a value well that's the same thing with these classes you know over here inside of this class declaration I'm basically just defining like what a student is giving a basic template of like here's what a student is inside of our program but this isn't a specific student right if I wanted to start using this I'd have to actually like create a student and give it all of these different values so I'm gonna show you guys how we can do that I'm gonna come over here into my program so I'm just going back over to this app Java file over here in this app file I can actually create a student so we can actually create one of these students that we defined over any other file so the way that I can do that is I can basically create it like I would any other variable so the first thing I want to do is specify the data type of the variable that I want to create in our case we're creating a student variable so I can say student and then I can just give it a name so we'll call this student like my student and now what we want to do is we want to say equals new student and then open and close parentheses so this is basically how we can create a new student and what we'll have here is an empty student so this student has no attributes associated to it so what we want to do is we want to start giving this student some attributes so I can basically do that by saying my student dot and now I can access one of the attributes that we defined about the student so I could say my student dot first-name and you remember in that my student class we defined a first-name and I could say equals so I can just say like we'll say Jim and we can do the same thing for last name so I can just copy this guy and instead of saying first name we'll say last name and we'll just give it a last name helper so now we're basically defining the first name and the last name for our student and we can do the same thing for all those other attributes so we could say my student major and we'll give them a major so business and what else did we have so we also had GPA and on probation and age so let's say that jim halpert's GPA he's probably kind of a slacker so it's just gonna be equal to two point three and then we can say my student age 24 and finally we'll say my student dot on probation and this can just be equal to false so what we've basically done is we created this student and what this is called is it's called a student object so what we defined over here in this student.java file we defined a student class and this is basically just like a specification for what a student is in our program so it's basically just defining like hey this is like what a student is it has a first name a last name a GPA a major has all these different things but when we want to create an actual student so like when I want to create a student and like give it different values I can create what's called an object and an object is an instance of a class in other words an object is just like that class that actually has values inside of it so it's like an actual student instead of just like the specification for a student so now we actually have this jim halpert object who is a student in our school so I can come down here and I can actually like print out certain attributes about this object so I could say like my student dot first-name and now this will actually print out the first name of our student onto the screen so let's go ahead and run this program I'm just going to click run and you'll see over here it's printing out the students first name which is Jim I can then print out their last name so now we should get Halpert I could print out any of the attributes about them so I could print out like the GPA and we'll print this out so basically what I'm doing is I'm creating like a student data type I'm creating my own data type and then I can create an instance of that data type or an instance of that class which is called an object and that's just like a particular student inside of my probe so if I wanted I could create another student so I could just copy this entire thing and we could just create an entirely different student down here so we could just call it like my student 2 or whatever and now we'll give this student a bunch of different attributes so we can make it like Pam Beesly and she majors in art and maybe she has like a 2.5 GPA and she's 23 and put her on probation so now we have an entirely different student inside of our program so if I wanted I could come down here and I could access information about that student so we could say like first name and here we're accessing that different student so here we're getting Pam and so the point is that I can define a student and I can like specify like what goes into a student we have like the first name last name the GPA and then I can create instances of that student inside of my program and I can use them so the class is this specification over here it's just like specifying what a student is and an object is an actual like instance of that an object is an actual student who has actual values associated to it so that's classes and objects this is kind of a lot to take in and I know if you don't fully understand what's going on like right now that's totally fine classes and objects are a you know sort of a more advanced topic in programming what you want to do is play around with creating your own classes and objects so I created this student class over here and I created student objects which are just like individual students here's my challenge to you guys take an object in your everyday life you could take like a phone or you could take a credit card or you could take a book and define a class about it so for example if I was defining a credit card class I could give it like the credit card number the expiration date the issuer of the card like Visa or MasterCard you could give it a security number or I could create a class for like a book we could say like here's the title the book and the author and the number of pages so you can create classes to model real world entities and then you can represent them inside of your code doing stuff like this so going forward we're gonna learn a lot more about classes this is just scratching the surface we're gonna learn how to make these way more powerful and make it easier to create them and stuff like that hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopper Academy and you want to help us grow head over to draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 47,933
Rating: 4.9776659 out of 5
Keywords: Programming
Id: Mm06BuD3PlY
Channel Id: undefined
Length: 13min 51sec (831 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.