Java Beginner Tutorial 10 - How to create Classes and Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello and welcome to this session on how to create classes and objects in object-oriented programming Today we are going to see how do we convert information into tables? Then how do we create a class and variable using this table? How do we create objects? And we will look at a very simple example of an object-oriented program in Java It is taken in the last session We have already seen how can we look into a scenario and find out the nouns or the entities, and then we can design or define our classes We have also seen how does an account holder looks like in real world? So for example, Tom is an account holder So is Henry and Sara Any account holder will have attributes or variables like first name, last name, age, account balance, and eligible for credit card And they can be some functions like testing their eligibility for credit card, deposit money, withdraw money, and so on In object code In programming, the same information is imitated in the class So an account holder class will have the same variables like first name, last name, Ah, account balance, eligibility for credit card, and we'll have the similar functions So if you take example of a Java class, the account holder class in Java will have all these variables and then all these functions We are going to look at how exactly we do it Practically Step 1 will be we are going to represent the account holder details in the form of a very simple table Let us create an account holder table and the column names will be the variables So we have first name, last name, each account balance eligible for credit card So if you want to put in the values for Tom, we will have first name is Tom, last name is Smith, his age and his account balance. Similarly, for Henry, we can put down his values And so for Sara So now we have details of all these account holders We do not know the eligibility for credit card So let us put in a very simple condition that if age is greater than 25 and account balance is greater than or equal to 20,000, then the person is eligible for credit card Now this is a processing So this will go into a function and then the results will come back to the table Let us see, how do we do it in Java? So step 2 is we will create a Java class with the name as the table name and the variables as the column names So here the table name is account holder So we will create a class by the name account holder, and we will add the variables as the column names Let us go to our eclipse and let me create a very simple package here, and I will call it classes and objects And under this package I'm going to create a new class and I will name it as account holder So we have got our account holder class and now we have to keep the variables So we had first name, last name, age, account balance eligible For credit card No, these are the variables we will give the data types The first name should be a data type, a string Last name should be string, age should be an integer Account balance can be a float number, and then this can be a pull in value So you can see we have simply taken the table and its columns and created a class out of it in edit our variables Now let us go to the next step Step number 3 is we have to create another class where we will create objects for the account holder class So let us go and create another class I will right click on my package, go to new Select class And here I can give the name as account holder objects And I will say finish here I want to have some function So I will have the main function You can give the name as mean press control space on your keyboard, press enter, and you will get the declaration of this main function And now it is saying that we have to create objects for account holder class How do we do this? So we have to give the name of the class, account holder and the object name For example, I want to create an object for Tom So I will say Tom equal to new account holder name of the class with the brackets So this particular statement create objects in Java Now we will go deeper into how this exactly works in the coming session But for now you can assume that this statement creates an object for Tom And Similarly we can create object for Henry I can give any name, so I will give Henry here and Similarly for Sarah So these statements have created 3 objects Okay, so we have created the object and now we will be giving the details of these objects or the account holder to the particular object So for Tom I will say firstname="Tom", Tom.lastname ="Smith" You have to give the semicolon the end to do age equals 21. Tom.account balance equals 10,000 And now we have this eligible for credit card So for this we should be having some function that will test this particular condition So let us go back to our account holder class and create a simple function to do this processing I will say public avoid test eligibility for credit card And here I will put the condition that if each is created than 25 and account balance is created in or equal to 20,000 then the person is eligible for credit card So the same thing I'm going to give here age is greater than 20 Sorry, 25 And account balance is created in or equal to 20,000 Then this particular variable should be true Okay, otherwise it should be false So now we have this function which will test the eligibility in the account holder object class I can now call this function for Tom by giving the object name and the function name So what this particular statement will do This will call the function test eligibility for credit card by passing the values for Tom So it will pass 21 and 10,000, and it will call this function Of course, the result should be false, so we can print that as well I can do this out You can also use the shortcut You can type SYSO and then press control space And then I will say Tom.eligible for credit card, and this should print as false I can also put some statement before it is Tom eligible for credit card, and they should print as false Let us now test it You can run this program I'm doing a right click run as Java application, and you can see the result is Tom eligible for credit card It is showing me false So we can do the same thing for Henry I will just copy this Paste this again and do this for Henry So I'm replacing Tom with Henry So this is the object for Henry Let me just replace all the tom's with Henry here as well And now I have to keep the details for Henry So first name is Henry Last name is Hill, age is 31 Account balance for Henry is 20,000 And now it will test this particular function with the details of Henry Sort will test this function with age 31 and account balances 20,000 And then it will print the result of whether Henry is eligible or not In this case, they should print as true Let us test it Let us run it again And Yes, you can see the results for Tom eligibility is false and for henry eligibility is true Okay So this is how we can convert our information to tables and then two classes and objects in Java So we have already ran this program and validated it And now we know the eligibility for credit card in case of Tom is no In case of Henry is Yes And of course, if we create an object for Sara, it will come as Yes Again So this is how you can think of object oriented programming Java and create objects and classes I hope all this information is very clear to you I will meet you in the next session, Thanks for watching.
Info
Channel: Automation Step by Step
Views: 36,238
Rating: undefined out of 5
Keywords: learn java, java classes and objects, java how to, how to create classes and objects in java, java example, step by step guide, java step by step, convert information into objects, first java program, java real example, java object oriented, object oriented programming, oops, oops concepts, oops example, objects in java, classes in java, oops real life example, what is oops, java beginners tutorial, object oriented programming beginners tutorials, java beginner step by step
Id: 8vpB3e43O94
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Sun Jan 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.