right so in our previous class uh we have seen what is a class what is object and how to create a class how to create an object and also we have discussed about how can we create variables and methods inside the class all those things so today we'll discuss more about methods Java methods so normally the methods will be created inside the the class okay all the variables and methods should be part of the class and uh what is a method so method is basically a block of or or group of statements or block of code uh which will perform certain task which will perform certain task so why we need to create a method suppose if you want to do some task repetitively and instead of writing the same code again and again we will just create one method and when whenever you want to perform the task we can just call that method okay and that method will perform the job so the normally the method will be created inside the class so the class is a collection of variables and methods so the methods also part of the class and whenever you create a method we can call or we can invoke that method through object so once you created your object through object we can call that method and then method will perform the job so whatever whichever logic we have written inside this method that logic will be executed only when you call that particular method okay so there are two kinds of methods which we have so we have a built-in methods we have a user defined methods buil-in methods user different methods built-in methods means those methods are already available in Java and there are pre def classes and those classes those methods are already exist so we have to just call those methods we want to use those method you have to just call but user defined methods means we can create our own methods we can create our own customized methods according to our requirement our own requirement and these are two kinds of methods which we have so first of all what is the definition what is a method as per Java the method is a group of statements which will perform certain task and when it will perform the task only just writing method is not enough when whenever we want to perform the task we have to call that method okay so we have to call that method then that method will perform the task or job and that method we can call through the object of the class once you create an object for the class through the object we can involve the method and there are four different type of methods are there sometimes it will not take any parameters parameters in the sense input and the written value means output of that method so some sometimes the method will not take any parameters but return some value sometimes it will not take any parameters but it will return return some value and sometimes it will take parameters but no return value sometimes it will take parameters but also return return some value so different ways we can create a methods it depends upon our requirement we have to choose these are the four categories we can pass parameters sometimes we don't need to pass any parameters sometimes method May return some value return means the output will be written and sometimes method may not return any output it will just perform the task that's it it will not return any value so there are many ways we can create different combinations of methods we can create and before creating let us understand what is parameter what is return value so let me explain this with simple buil-in method so we already discussed about some of the built-in methods in our previous classes string Methods we have discussed all string Methods are available inside the string class so we can use all the string method they are all buil-in methods so let me take one uh example let's say I'm taking one string variable equal to new string so what exactly have done here is I created a simple object of a string and here I'm passing some value this is my string variable this is my string variable or string object which contains some value right so now if you want to find length of a string okay so this is a value actually so I want to find length of a string can I call methods from the string class yes because this variable is what string type or we can say this is an object so s dot length when I call length method from the string class will it take any parameters is it taking any parameters this particular method is taking any arguments or parameters no parameters why because it is an empty bracket and is it returning any result for us any output for us yes it is rning length of a string so whenever a method is rning something we can store that value in some variable like this or we can put this entire thing in the print statement so that we will get some output so this is an example so here this is an example for what in which category this method comes under in four categories no parameters but return some value for us no parameters return value so this is belongs to Second category okay this is no parameters but it Returns value okay so this is one example let me take another example for the same same string variable I want to extract the substring of the main string so if you want to extract the substring so what is is the method we have to use S dot substring of let's say 2 comma 5 now look at this method so is it taking any parameters yes two parameters we are passing starting index ending index two parameters it is passing so this method is taking parameters and is it returning some value for us is it rning something yes it is also rning substring so return value we have to store some variable like this okay so this is a way we can create uh these are the built-in methods they are already there in the string class so through this object through this object we're able to access all the string Methods some of them will take parameters some of them may not take parameters some methods May return some values some methods may not return any Valu so depends upon our requirement we can create our own customized or user defined methods okay now let us uh try to create different type of methods by considering these points no parameters no return value no parameters return value no Tak parameters and no return value take parameters and written so different combination of methods we can create okay so normally where we have to create a methods in the class we can create and to access those methods through the object of the class we can access right now let us see some examples how can we create methods with the different type of combinations let's go to eclipse and create new package raise dat 11 on finish now here I'm just creating a class without having main method let's create main method in the separate class or you can have main method in the same class no problem so let's take a new class and I'll name it as greetings my class name is greetings I'm not taking main method a class and normally class contains what variables and methods but not mandatory okay if you have some variables you can create variables and if you have some methods you can create a methods but if you have if you don't have any methods I just have only variables you can Define the variables or if you don't don't have variables I have only methods then you can defend only methods okay so there are no methods available as soon as you create an object of the class when you create an object dot it will show you all the methods okay for example uh if you create a string class suppose if you create a string variable like this right so as soon as you say yes dot right it will give you all methods Auto suggestions what are all methods are there in in the string class similarly it can create object of any class okay and if you say object name dot it will show you list of methods available in that particular class so that is the only way we can uh find all the methods in the class okay but that is not needed actually we don't need to remember each and every method in the class there are hundreds and thousands of methods are there in every class so it is very difficult to remember just to have few methods once you started using frequently you will able to remember them or if you still not remember you can simply say object name Dot and you can just type one or two characters first or two characters it will automatically suggest you what all methods are there okay but there is no specific command or specific method to check all the methods available in the class no okay now go to the new class greetings and inside this I'm just creating uh some method this is a I use this class just for creating methods okay I don't need any variables for now okay I just create some methods and to access those methods we have to create an object of the class right so where we have to create an object normally where we have to create an object of the class where we can create inside the main method right so whichever class is having main me method that is called main class so we have to create another class you can also include main method in the same class no problem but normally we don't do like this we will always maintain separate main class so I will name it as uh I name it as greetings main class greetings Main and here I'm taking main method okay now I have two classes one is greetings class and another one is greetings main class which contains the main method so whenever you want to create an objects all objects should be created inside the main method so inside this main method we have to create everything so let us try to use this main method later so now come to the m greetings class here I will create a different methods different type of methods as I said there are four ways we can create so the first method no params no return value no parameters no return value so how to create this method no parameters no return value okay let me create one method M1 okay so this is one method M1 you can give any name to this method there is no mandatory to uh give same name or a different name you can provide any name this is just a user defined name so M1 is just a method name I inside this I'm not taking any parameters no parameters means empty bracket no parameters means no uh parameters also called as arguments it's a different name so no parameters or no arguments now insert this method I will write the simple thing called system and I can simply say hello so this is a simple method this is a method name Open Bracket means no parameters and what this method will do when I call this method it will just print hello it will just print hello so normally in the methods we will Implement some Logics so when I call this method it will just print Hello message and uh it is not returning any value so if it is not returning any value means we should specify void so in Java void what is the meaning of void is nothing so if any method is not returning any value in front of this method we have to specify void but sometimes the method May return integer method May return string or any other data types in those cases we have to specify the data type what type of data the method is rning so that return type we have to specify in front of this method okay but from now this method is having just a meth print statement it is not returning any value it is not taking any parameters right so empty bracket is representing no parameters void representing no returned value of this method so now I have simply created one method now come to the main class how to access this method only through the object so we have to create an object and then we can access this method so now come to the main method inside the main method we can create an object of gretings class right let's cre greeting gr equal to new gretings now I created one object so as soon as we created one object this object will be acquired everything from this class okay object is an instance of a class that means this object contains the M1 method whatever method we created inside the class same method is belongs to gretings now can I invoke this method by using gr gr is a just a name of the object now next how to call that method gr dot you can see as soon as we said dot it will show you what are the list of methods which are available in that particular class which method we created M1 so you can see M1 method is already there in this particular Auto suggestions other than this user defined method there are some more additional methods also displayed in this Auto session because whenever you create a class Java will automatically add some default methods okay so we don't consider them we can simply ignore and here this is a method which we have created just we have to select that method okay and whenever you call this M1 method this will go and execute this particular statement this body will be executed and uh it is not taking any parameters so we don't need to pass anything in the bracket and it is not returning anything so we don't need to maintain any variable here okay we don't need to create any variable to store The Returned output so simply we can leave this method like this no parameters no return value so this method will call the first one when I execute this run as Java application so this will execute and just print hell message so this is how we can simply create one method no parameters no return value and we can in that method from the main method through the object this is one example now let me create another type of method come to the main class so the next type is no parameters no return value sorry no parameters but it will return some value the next one no parameters but it will return some value this time so what I'll do is I will create another method void M2 and you should not use same name again and again okay so the method names should be different but again some cases we will use same names for multiple methods we can use same names that is part of the overloading concept okay that is we'll explain later but by default normally when you create a methods the method names should be different okay so now I'm giving M2 so this time no parameters so empty bracket but I will return some value so what I will do is previously what we have done we have just printed the Hello message okay just we printed Hello message so this time I will instead of printing this I just want to return the message for returning we use one keyword called return return and uh here I will specify hello how are you like this okay and whenever you're rning something here you have to specify the written type so what type of data we are rning what type of data we are returning here and this bracket is optional okay so after returning this bracket is optional not mandatory you can put back it no problem we can put this back written statement will be used to return any value from this method now the return type is string so we are returning this double quotation value that is a string actually so the return return type should be string the return return type should be string so now if I look at this method it is not taking any parameters but it is rning some output so return return type is specified now how to call this method so to call this method again we have to use same object creating dot you can see M2 is displayed this time so take M2 so should we leave this or should we do something else just we are calling M2 method so it is not taking any parameters so we are not passing any parameters but if you call this M2 method it is rning some output for us right because it is rning a string type of an output so whenever a method is written in some result or some output we have to hold that result in a variable so we have to additionally create some variable here and we have to hold that output in a variable so this particular method is returning string output so we can create our string variable and then you can store that output okay but uh if method is not returning anything no variable is required if method is rning something then we have to hold that value or we have to cap that return return value in a variable and after that we can simply print that value say yes now when execute so this method will be called M2 method will be called now we got a message so whatever the value we have written here we just printed that one and the variable is optional suppose if you don't want to use this variable you can put the statement in the print print L and you can directly keep it inside so instead of yes you can directly put this inside the print LM so whatever value written by this M2 will be printed by print L okay so this is how we can call a method which is returning a value so two different methods we have seen method no parameters no return value no parameters but it will return a value now the third one takes parameters but no return value it takes parameters but there is no return value the third method third type of method sometimes method will take parameters but it will not return any value okay now let's create one more meod M3 so M1 M2 is greater now M3 so takes parameters so let me take you can pass any number of parameters you can pass any number of parameters so takes parameters so here I'm taking one string type of parameter string some person name I will take as a parameter so this is a variable which will hold the value which we are passing to the method and this will receive the parameter and inside this it will not return return any value no return return value no return return value means we have to just print so I'm just printing here I say hello and concatenated with what the name so whatever name parameter I'm getting here I'm just printing the name along with the greeting message and uh it is not rning anything so what is the return type here void is a return type so if I look at this this method is taking some parameter string parameter it is taking and uh it is not rning anything void means it is not returning anything but what this method is doing whatever parameter it is taking it is just printing the same thing along with the greeting message we just passing the name and it is printing hello along with the name this is the implementation now let us invoke this method through the object we have to call greetings gr do M3 okay so this time we have to pass parameter because this is expecting a string parameter a string input it is expecting so here we have to pass a string output in the double quotations okay so here you can pass some name like this so if method is not expecting anything no parameters so we don't need to pass just we call this method with empty bracket but if the method is expecting some parameter we have to pass this parameter okay so okay now we have passed this parameters is it returning any value for us there is no return value there is no return value so we can just leave this method as it is we don't need to again print by using print statement and we don't need to receive the output in another variable not needed so simply execute okay now can see we are just passing the name but it is giving message so this is this is the statement which is got executed so this particular value which we are passed and stored into this variable and this parameter is received this variable is received that parameter whichever we passed here and internally it is just printed the same name along with the greeting message so return type is void this is another way we can create a method Tes parameter there is no written value sometimes we can pass pass multiple parameters also not only single parameter multiple parameters also we can pass and if you want to pass multiple parameters you can simply say comma after this comma you can specify n number of parameter at the same time whichever order we specifies in the same order we have to pass the parameters okay remember this now next type of meod takes the parameters and also returns a Val value takes parameters and also Returns the value the next method takes parameters also Returns the value same method I'm taking again let us copy this let's name it as a M4 and it is taking parameters I'm passing name as a parameters and instead of printing this value I'm just want to return it so remove this print and say return so return this string greeting message along with the name after combining after conation I'm rning the final string so what is a return return type here is string is a return return type string is a return type so this is another method which is taking one parameter and also Al returning some output so to invoke this method we can simply say gr do M4 and it is expecting one parameter so we have to pass some string type of parameter and also it is returning some value we have to store that in a string variable we'll create one string variable and then we can print that particular variable so we are pass passing parameter and also we are receiving the output into the variable so after receiving the output from the variable into the variable then we have to just print it so this is a how we can get the output okay so if you take any method which is belongs to one of this category it can be built-in method or it can be user defined method anything it should comes under one of the category sometimes no parameters but return some value no parameter but return value sometimes it will take parameters but no return value also sometimes it takes parameters and also return return the value so according to our requirement we have to write our own customized methods these are all user Define we created our own methods in the class and through the object we can call whichever method you need so this is all about methods concept we can create different type of methods inside the class okay now what is the method the method is a group of statements or we can one statement sometimes group of statements which will perform certain task when the method will perform the task whenever you invoke that method or whenever you call that method it will perform the job how to call that method how to invoke this method through the object through the object we can invoke the method remember these points now so let us discuss some other concept so the class contains a two things variables and methods so far whatever we discussed about all methods so how many ways we can create a methods how to invoke those methods now let us discuss something about variables okay class variables how many ways we can store the data into the variables in the class there are many ways to do it how many ways we can store data into variables and uh there are normally three different ways by using object we can directly access the variables and we can store the data the last class also we have seen other than this we have a method through Constructor also we can do it okay let me show you directly with the example so now we'll try to understand with the variables the class variables create a new class and I'll name it as a student class and this is purely class I'm not specifying any main method so inside this student class uh I will create three variables I can say in Sid which will store student ID string yes name it will store name of the student tab grade so this will store grade of a student so three variables I'm taking and I will assign the data into the variables to the object and after that I want to print this data so print this student data what we can do to print the student data we can write one more method okay I can say V print student or St data or print student data and this is just for printing the student data I'm just writing one single method for printing the data first I'm printing Sid then uh concatenation with space and then concatenate with this name then again I'm concatenating with the space and then I'm printing grade app single statement I have written to print all the details of students so this is the implementation of the class three VAR variables I have defined and just one method is defined which will print the data of the variables okay now I will create another class student main class student main class and here I'm taking main method okay now we'll see something here this is my student class and this is my student main class so can we create an object of student class yes we can create an object of student class and then we can access all the variables through the object right we can directly access this variables through the object and we can assign the data into the variables so as soon as you created an object how to create an object of student student I say uh Stu equal to new student so this is how we will normally create an object so once you created an object object then what will happen it will create a separate memory so as soon as it says new student it will create a new student object the name of the object is Stu so what is this object contains three variables S ID s name grade along with this there is a method called print student data so this is our object so through this variable we're able to access everything so we can access the variables and we can access the method everything we can access through this object reference variable so if you want to assign the data into the variables how can we assign directly st. Sid S.S name st. grade we can directly access this variables and we can assign the data this is a first approach how to store the data in a variables the first approach is what through object reference variable okay okay how to store the data in a variable the first approach is what through the object reference variable okay let me store it here the first approach store data using or you can say using object reference variable we can directly access the variables and store the data so how can we do it St dot can see S ID I say some data St dot yes name I can store some name St do yes name then St dot grade this is a character so I can put in single qus so this is a one approach we can directly access the variables through the object and we can assign the data into the variables and after that we can simply call that method St do print student data okay so like this we can simply uh store the data in a variables and after that we can just call that method which will print the data of the student now so when I run this so you will get the data of the student so this is a normal way uh we will follow so we directly access the variables and then assign the data to the variable so we are directly accessing it there is no intermediate so we are directly accessing the variables through the object and uh we are asscending the data or we are storing the data into the variables this is a first approach now the second approach is we can also store the data in the variables through the method through method okay for example here along with this print method I will create one more method and that method will just store the data into the variables this method is using for just for storing the data into the variables okay so for example here I'm creating one more method void void set student data set student data and it is is not returning any value but here I will take three parameters okay one is integer ID comma string string name comma character grade gr so I'm taking three parameters inside this method three parameters okay first parameter is ID second parameter name third parameter is a grid to store grid I'm taking three different parameters ID name and grid so once you take these three parameters now what I will do inside this method I will store this data into this variables okay simply what I can do is S ID equal to ID okay s name equal to name grade equal to gr like this so what I have done in this method I'm taking three parameters and uh whatever data I'm passing into this method I will store the data in these three parameters first and after that I will assign the same variables data into actual variables they are Sid s name and and if you look at here these variables are called class variables and here whatever variables we created these variables are called local variables local variables these me these variables are accessed only within this method within this method only we can access this variables but the class variables we can access everywhere inside the throughout the class in every method we can access these variables they are class variables and whatever variables we created here they are called local variables or method specific variables okay so now I have stored the data in this variables and again I'm reassigning the same variables data in the class variables okay now this is one method okay so now I will what is the purpose of creating this method to store the data in the variables okay now how can we call this method from the main class so before printing the data okay the first method is over now I'm talking about the second method comment this pattern second method is what using a method we can store the data using method so we already created one additional method here just we have to call that method sdu dot set student data now we have to pass three parameters so what is a first parameter student ID so one1 comma second parameter name that is a string so we have to put in the double quotes now the third parameter is a character that we have to put in the single course so in whichever order you are receiving the parameters here in the same order we have to pass the data so first one is integer here here also first one should be integer second one is a string so here also the second parameter should be string third parameter is a character here third parameter should be character so in whichever order you created this variable exactly in the same order we have to pass so now what will happen this particular data will store in this variables whenever you call this method it will call this method and the data will be assigned to this variables and then internally what this method will do it will just assign the data into the variables it will not print anything it will not return anything so what is the main job of this method it will just take the data and stored the data into the variables that's it that's the only purpose of this method so once it is stored the data then we call this a print data method so this will actually print the data of the variables so we have created some additional method to store the data in the variables and that we have called and uh then to print the data of the variables we have called another method so using the method also we can store the data in the variables in the previous approach what we have done we directly access the variables and Stor the data but this time how we have done we just call one method by passing the data and this particular method is assign the data into the variables okay now when I execute this it will call set to student data method first and once asend the data and then we call second method called print student data so that is got printed same data so whatever data we are passing here the same data is got printed by print method so this is how we can also store the data in a variable so the first approach is by directly accessing the variables through the object we can store the data and variables second approach is just by creating one more additional method we can also store the data in a variable so what is the purpose of of the first method it will just print the data of the student what the purpose of the second method it will store will take the input and store that input or store the data in a variables so this is how we can store the data in the variables one is by directly accessing the variables through object second is by just creating one additional method we can store the data in a variables okay so now let me read the question can simplify perform action from first method when we use second method yes we can also combinely write one single method also no problem okay so one method for taking the storing the data and printing the data suppose set student data is there so if you put the statement after this what it will do first it will store the data immediately print the same data by using this method we can also write a single method no problem in that okay but normally we don't do like this everything we don't put in one single place okay it will miss the readability of the code so for different purposes we will create a different methods okay suppose uh you want to just store the data I don't want to print it then how can we achieve this so if you just print the statement if you put the statement inside this okay suppose if you want to store and print at one time you can call the method but my requirement is different I just want to store the data but I don't want to print then how can we achieve this so that is a reason for setting the data or storing the data we create one method for printing the data we will create another method so whenever you store if you want to store the data call one method and whenever you want to print the data you can call another method so that is how we can create so why these local variables are reassigned into the class variables why we are doing like this because we need data in this class variables not in the local variables the local variables we have used only to receive the data which we are passing from here you can see here they're passing the data right so whenever it pass the data to receive and store we need some variables here so this ID 101 should store in ID here and David that name should store in the name here the character a should store in this variable so so whatever data we are passing here to store the data we need a local variables first we will hold the data in a local variables after that we will actually store the same data in the class variables so whatever data we specified variables again we are reing the local data into the class variables so that the reason we need this local variables we need this local variables okay and these are the class variable these are the local variable Ables and here the names are different you can just look at here I have given different names to local variables class variable names are different but sometimes we can also use same names local variables and class variables but that time this method will confuse actually so what is local variable what is class variable so that's the reason we can specify the different variable names but it can also give the same names sometimes but we have to differentiate them by using this keyword so we have to use a separate keyword for that in the next sessions I will introduce that but for now just remember this so why we need to specify the variables here because we are passing the data into this method whenever you pass some data we need some variables to hold the data okay and then again whatever data we have in these variables again we have to reassign the same data into the class variable what is our ultimate goal ultimate goal is we have to have data in the class variables not in the local variables so that's the reason we are using this local variables temporarily to store the data whatever we are passing from here and then we are reass sending the same data to the class variables that is our ultimate goal okay so then we first call Print set to student data method which will set the data in the variables then print the data from the variable so without setting the data don't print it you will not get anything okay first we have to set the data then print the student data so is this clear to everyone please confirm in the chart window so there are two approaches I have told you the first approach using object we can directly access the variables and assign the data second approach is what we can create our own user defined method which will get the data and which will assign the data into the class variables this is a second method first method is through object second method is using a method will the data set by calling St method not store directly in the class variable no it will not store directly so directly means this is the first approach we need to go to the first approach we directly want to store the data in a varibles you directly access through the object and directly store it no method is required okay so here we are just calling this method by passing the data and this method is storing the data in the class variable and repeat it multiple times guys some people may get some Bor but please have some patience but still people have not understood this so here this method is storing the data in the class variables you got my point Diva so set to student method is storing the data into the class variables it is a direct process it's not indirect it's a direct process when you call the set student data what we are doing we are passing the data immediately what happens the set student method data will be called take the data in this variables and what this method is doing this method is storing the data in the class variables internally so the set data method is directly storing the data into the variables and this data we are passing from the main method we are passing the data whichever data you want to pass you can pass okay now the third approach this is second approach through the method now the third approach is using a Constructor using a Constructor there is a third approach using Constructor also we can store the data in a variables so now we need to understand what is Constructor con Constructor is also part of the class so far what we have understood is a class is just a collection of variables and methods and the classes also have Constructors Constructor is just like a method but the way of writing and way of calling is different but it is just like a method whatever method is doing Constructor is also doing the same thing so what is Constructor Constructor syntax exactly same as a method and Constructor name should be same as a class name there's a major difference the method name can be anything we can provide any name to the method but the Constructor name should be same as a class name so here I will create one Constructor let's see student bracket and bracket can see if you look at the syntax it seems very similar to the method okay but it is a Constructor why because the name is class name whatever the name you have given to the class same name you have given here that's one difference between method and Constructor another difference is the method may or may not return any value sometimes it may return value sometimes it may not return any value but Constructor will never return any value Constructor cannot return any value that is another major difference between method and Constructor Constructor will not return return any value not even void if it is not written any value normally we specify void right in case of Constructor no need to specify void also if you specify word nothing will happen but even we no need to specify void also so return type is not compulsory even void is also not compulsory just write a Constructor like this this is a major difference the method name should be same as a class name and Constructor will never return any value not even void okay remember these two points but Constructor can take parameters just like a method so how we have passed parameters inside this method in the same way the Constructor can also take parameters now I'm passing some parameters to this Constructor three parameters now what is Constructor is doing Constructor is Al assigning the data into the varibles so same thing I'm doing here Constructor whatever the method is doing here the same thing Constructor is also doing here it is taking some parameters assigning the data into the variables okay so this is a Constructor and one more difference is in methods we normally Implement Logics we can write conditional statement looping statements everything we can include in the methods but Constructor is only meant for initializing the data in the variables we should not write anything inside this Constructor other than variables assignment we can just for storing the data in a variables we use Constructor just for initializing the data into the variables not nothing else we should not write any loopings and conditions any programming Logics we should not include in the Constructor so the purpose of Constructor is totally different than method even though it is almost similar syntax the purpose of creating Constructor is totally different so now let me just compare the method and Constructor very very important interv question also what is the method what is a Constructor what are the differences so far what we understood the method name can be anything whereas Constructor name should be same as a class name that's the first thing second Point method May or may not return return any value but Constructor will never return return any value the second difference what is the similarity methods can take parameters similarly Constructor Also may take parameters and third thing is what con method can be anything so in the method we can write anything but in the Constructor we can just initialize the data we don't write anything else construct is only for assigning the data into the variables nothing else okay so this is the way these are the differences while writing the method and Constructor right now come to the main class there is no specific reason actually the purpose of Constructor is only for initializing the data so for that reason they have created The Constructor if you write some logic here then you will able to return return right but we are not going to write anything it will not accept to write any la Logics so that is the reason there is no need of returning any value if you write something here if you write some logic then you can return some output right but first of all in Constructor we never write any Logics so in that case there is no need of returning any value right so that's the reason they have created Constructor Constructor is only meant for initializing the data into the variable that's the only purpose of Constructor nothing else but in the method why return value is required because sometimes we write some Logics also and when you write some logic inside the method that will return some value so return is must for methods but Constructor is not needed first of all because we're not writing any Logics until unless you write some logic there is no return value so obviously no return value for the Constructor okay now how to invoke this Constructor how to call the Constructor very important part listen this if it is a method we have to call this method explicitly through object okay expc through object we have to invoke this method but how to invoke The Constructor to invoke The Constructor we no need to call the Constructor at the time of object creation itself The Constructor will automatically involed okay that's the major difference between method and Constructor if it is method we have to invoke through object we have to call this method through object if it is a Constructor this will automatically executed at the time of object creation whenever you create an object The Constructor will automatically invoked you don't need to invoke so at the run time the Constructor will automatically invoke and assign the data into the variables and one more thing as soon as you write your Constructor here here it is started giving an error have you look at here in the main class here it is giving an error previously it was not there now it is started giving error why because at the time of object creation it is trying to invoke the student Constructor when you trying to invoke The Constructor it is expecting three parameters but here we are not passing any parameters right it is expecting parameters but here we are not passing any parameters that is the reason it is giving an error okay so what I should do now we should create an object with parameters so at the time of object creation it will automatically invol but because of it is expecting three parameters we have to pass parameters inside this bracket like this okay so how many parameters The Constructor is expecting those any number of parameters we have to pass so at the time of object Creation The Constructor will automatically invoked so the data will be assigned to the variables at the time of creation object itself so we no need to separately call that method we don't need to separately assign the data into the variables so that is the main advantage of using Constructor so next thing is what once the data is assigned into the variables through Constructor what is the next thing we have to just print the message print the data by calling another method s du do print student data that's it so this will print data of the student like this so this time we have not called this set student data method we have already commented this code we just created an object of the student class passed some data and Constructor is automatically invoked at the run time and after that we just printed the details by calling print store data method so this is another way of assigning or storing the data into the class variables we can directly access them through the object that's one approach by creating user defined method we can pass the data that is another approach by writing the Constructor also we can assign the data into the variable so these are the three different approaches and which approach we have to prefer to use most of the times we prefer to use Constructor to assign the data into the variables okay Constructor will be used most of the time to assign the data into the variables you always try to use Constructor why because at the time of object Creation The Constructor will automatically invoked okay and that is only meant for what initializing the data into the variables nothing else okay when we have to prefer method when we have to prefer Constructor if you want to assign the data into the variables Constructor will be preferred and if you want to operate these variables or if you want to perform some operations based upon this data then we have to prefer the method okay inside the method you can operate this variable you can do some calculations or you can write something Logic for that we will prefer to create a method and to assign the data into the variables we always try to prefer Constructor okay remember this point method is for creating or implementing the logic or programming code or programming script Constructor is for what assigning the data into the variables that's it so these are the three different approaches which we have to store the data into the variables everybody's understood now I will show you what is Constructor I will discuss more about Constructor I've just introduced what is Constructor I will see more examples can we write multiple Constructors yes we can write but we should change the parameters same Constructor you cannot write one more time it will be duplicated okay if I look at here okay so if I look at this Constructor one more time like this it will be duplicated can simply say it is a duplicated but you have to change these parameters one parameter or two parameters you can take I will show you that also possible that is comes under overloading concept there I will show you yeah the method equal to assignment plus operation yes you can do like that assignment you can do along with operation also you can do method but Constructor is only for assignment not for operation okay and is it mandatory to initialize the data parameters in Constructor method only or we can initialize inside the method that is up to you that's the reason I told you two different approaches using method also you can do using Constructor also you can do but most of the times we will prefer to do it using Constructor okay but in the method assignment also we can do operations also you can do but in Constructor only assignment you can do not for operations because the main purpose of the Constructor is initializing the data into the variable at the run time that's the reason whenever you create an object the data is also assigned but only you have to make sure you have to pass the data only just should not leave the object like this whenever you create an object like this he can see here I'm passing the data also okay if I don't pass the data then you cannot create an object because there is a Constructor inside this object inside the class so whenever you create an object Constructor is trying to invoke okay and whatever parameters it is expecting we have to pass the parameters also so these are the three different approaches which we have to store the data in a variables by using object reference variable by using a method by using a Constructor so all these three Es are clear everyone please confirm in the chat window by using object reference variable using method using Constructor I will discuss more about Constructor now Constructor is a totally different concept we will try to understand okay so now let us compare very very important question in interiew what is the difference between method and Constructor what is the difference between method and Constructor I will put all the points just remember this and also I go through this points difference between Constructor and Method method name can be anything whereas Constructor name should be same as a class name okay method name can be anything but Constructor name should be same as a class name method may or may not return a value depends on your requirement you can create your method sometimes it may or may not return any value but Constructor will never return any value not even void so you know need to specify void also in case of Constructor and we don't specify void if method is not rning any value we specify void in case of Constructor we don't need to specify void and the method can take parameters or arguments similarly Constructor also can take parameters or arguments this is only one common point and uh if it is a method we have to invoke or call methods explicitly through object explicitly means we have to intentionally call that method through the object object first we have to create through object we can invoke the method explicitly in case of Constructor no need at the time of object creation itself the Constructor will be automatically invoked at the time of object creation The Constructor will automatically invoke you don't need to call it through object at the creation of object itself it will automatically and methods are preferred to use for logic if you want to write a logic code or programming logic you have to prefer always method and Constructor is only meant for initializing the data into the variables Constructor is only meant for creating the variables initializing the data into the variables so these are the points you have to remember related to method and construct so without parameter also we can create a Constructor I will show you that in the next examples okay so just I have given some basic information about the Constructor so far it is clear or not everyone yeah so now let me elaborate a little bit about Constructor so there are two kinds of Constructors one is default Constructor second is parameterized Constructor okay default Constructor and parameterized Constructor so what is default Constructor what is parameterized Constructor simple example if the Constructor is not taking any parameters we call it as a default Constructor if the Constructor is taking par parameters mean that's called parameterized Constructor so whatever Constructor we created here which type of Constructor it is default or parameterized which type of Constructor it is it is a parameterized Constructor okay because we are passing the parameters so let me give one more example let's create a new glass Constructor I'm also taking main method in the same class if you want you can take another class no problem okay let me just keep this main method one side now in this class I'm going to create one Constructor so when you create a Constructor the name of the Constructor should be same as a class name right so take the same name Constructor demo and this is called default Constructor no parameters and what's the purpose of this Constructor for assigning the data into the variables so here I'm taking two variables int X comma y two variables I'm taking and this is a default Constructor so what I will do is I will assign directly data into the variables so this is called default Constructors no parameters but it will do some task right what is the task the Constructor can do only initialization so in X and Y I'm directly assing some data so this is called default Constructor no parameters and now I create another Constructor and here I'm taking two parameters I say in a comma int B two parameters I'm taking and here a value I will assign to the X and B value I will assign to the Y this is called parameterized Constructor parameterized Constructor okay and to perform the operation I will create one more method and to perform the operation suppose I want to find X Plus y the sum of two numbers I should not put that in the Constructor so we have to create a separate method okay let's say I'm creating one more method called sum so what this method will do it will just return some value or it will print X+ y some of two numbers and what is the return type of this method what is the written type of this method why because we are not rning anything just we are calculating sum of two numbers okay so default Constructor parameterized Constructor and one more additional method we created to perform the sum of two numbers x + one now come to the main method and let's create an object of Constructor demo observe very carefully you guys can tell me this answer Constructor demo CD equal to new Constructor demo now I created an object or not I created one object and one point I told you Constructor will automatically invoke at the time of object creation now I have created one object here and I have two Constructors now which Constructor will be executed first one or second default or parameterized which Constructor will be executed yes default default Constructor if you're not passing anything here default Constructor will be executed okay because we're not passing any parameters and here also there are no parameters okay so this will call default Constructor and assign the data into X and Y next to if you want to find sum of two numbers what we can do dot sum we can just call sum method which will perform sum of x + y so when I execute you can see it is printed 300 so this will invoke default Constructor okay now we performing some now observe this suppose I want to when I invoke the default Constructor it will always take only2 200 but I want to pass the data dynamically whichever number I will provide it will perform some of those two numbers then you can use a Constructor demo another type of Constructor which is parameterized construct so here I'll create another object with one object you cannot invoke all the Constructor okay so Constructor demo DV equal to new Constructor demo and here we have to pass parameters let's say I can pass 10 comma 20 so this will invoke parameterized Constructor okay now you can call some immediately you can call at a time only only one we can enable Okay either this one or this one if you want to invoke both you have to create another object you can say here cd1 here cd2 like this you can specify but with a single object you cannot invoke all the constructors so when execute now we got a 30 so if you want to add 100 and 200 static values you can invoke the first Constructor and if you want to add some other values whichever you want to pass dynamically can invoke the second Constructor so you can create multiple Constructors also one is a default Constructor you can also create parameterized Constructor okay understood everyone all these points are clear what is the difference between method and Constructor please confirm in the chart window everyone is this clear about Constructor and tomorrow again we will discuss more about methods and Constructor we have overloading concept we can create multiple methods with the same name similarly we can also create multiple Constructors with the same name so that concept comes under polymorphism that's another type of object oriented programming concept so that we will discuss tomorrow session this is a basic understanding what is Method what is Constructor how many ways we can create a methods how to invoke the method how to create a Constructor how to invoke The Constructor okay so this is all about Constructor and Method okay what is a method method is a a block or group of statements which will perform the set task and we can invoke the method through the object and also we have seen different ways to create a methods no params no return value no params return value takes parameters no return value takes parameters Returns value and these are the three different ways we can assign the data into the class variables by using object by calling a user defined method and by creating another Constructor and also we have understood what is the differences between method and Constructor when we have to create a method when you have to create a Constructor if you want to write Implement a logic then you can use method if you want to assign the data into the variables then prefer to use Constructor okay and Method name can be anything Constructor name should be same as a class methods May return some values but Constructor will never return any value but what is the similarity between Constructor and Method only one similarity what is that can you guys noticed what is a similarity only one similarity we have between Constructor and Method yes it will take arguments parameters method also can take parameter Constructor also will take parameters but method can also return a value Constructor cannot return a value okay and one more major difference is what if it is a method we have to call through object if it is Constructor we know need to call actually at the time of object Creation The Constructor will automatically inbook we don't need to call the Constructor so these are the important things which we need to understand from classes and object so we have understood what is class what is object what is Method what is Constructor so these are the fundamentals in objectoriented programming con So based on this we will try to understand other type of objectoriented concept like polymar ISM overloading encapsulation inheritance so all these topics are based on this topics which we have discussed yesterday and today's session okay so just practice this this much is enough for today's session and try to understand this concept concept and be familiar with this and tomorrow session we will discuss about overloading so that is also based on method and Constructor so tomorrow session we will continue
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact