So in our previous classes uh in object oriented programming so we have seen uh two main Concepts uh one is class other one is object and uh we also discussed about methods which are part of the class okay and today we'll discuss some more objectoriented programming Concepts so we'll start with uh poar so polymorphism is a one of the concept in object oriented Programming now this is third concept so we have discussed about class then object now this is polymorphism so what is a polymorphism so let us understand simplest way and then we will apply
this in Java so poly means many poly means many morphism means forms polymorphism means one thing can have many forms one thing can have many forms so that is the meaning of poly marm one thing can have many Forms so what does it mean let us see some examples one thing can have many forms that's called polymorphism for example let me take a small word called shape shape shape is just a term there are n number of shapes are there for example cir is one type of shape and square is another type of shape and
triangle is another type of shape rectangle another type of shape so there are multiple shapes and one thing can Have multiple forms so one thing can have a multiple forms all these are comes under different shapes suppose let me take another example let's say water so water can have in multiple forms let's say Whopper will be there in vapor form right so water will be there in ice bulb form like this so one thing can have a many forms so that is meaning of polymorphism so poly means many morphism means forms polymorphism means One thing
can have a many forms okay and we can achieve this concept in Java using overloading we can achieve this concept or we can achieve polymer polymorphism is a theoretical concept a theory which we can achieve by using overloading concept overloading is a practical Concept in Java polymorphism so polymorphism is a theoretical concept which we can achieve by using uh polymorphism using Overloading overloading concept so what is overloading overloading is but a concept uh through which we can achieve polymorphism okay so this overloading is applicable for methods as well as Constructors methods as well as Constructor
so what is polymer of one thing can have a many forms same thing is applicable for methods and also Constructor what does it mean over loading is nothing but we can create multiple methods with the same name we Can create multiple methods with the same name in the same class normally when you create two method one method two two two times it will be duplicated right suppose I created one method called M1 like this and in the same class if I create another method like this it will be duplicated right but I want to use
same name but a different methods I want to create like this I'll create different methods with the same name but If if we create the same method again and again it will be duplicated but how can we achieve this we can simply change number of parameters of every method or order of parameters of every method or data type of parameters for every method so we can differentiate them okay so like that we can achieve polymorphism which is called overloading method overloading especially means method overloading means we can create Multiple methods with the same name but
we have to change the data type of parameters or the number of parameters or the order of parameters based upon the parameters we will differentiate these methods but the name of the method is same this is called method overloading so we can create multiple methods with the same name which is called as overloading so one thing can have many forms same method M1 which is is having different Forms one M1 method will take two parameters another method will take three parameters another method will take two parameters but different data types so one method can have
different forms one thing can have a many forms which is called overloading through overloading we can achieve polymorphism polymorphism is a theory concept overloading through overloading we can achieve polymar ISM in Java okay so the overloading is nothing Overloading is applicable for methods as well as Constructor you can call it as a method overloading if you apply overloading to the methods and if you apply overloading for the constructors we could call it as a Constructor overloading okay the main concept of overloading is one thing can have a many forms one method we can create in
multiple forms one Constructor we can create in multiple forms same Constructor name but we will Create multiple Constructor with the different type type of parameters called as overloading concept okay but when you overload a methods we have to follow four different rules applicable for methods as well as Constructor four rules we have to apply when you overload methods the first rule let me write it here the first rules first rule for overloading method names should be same okay Whatever methods we created inside the class all methods names should be same that's the first rule and
uh to differentiate those methods because if I create multiple methods with the same structure it will be duplicated it is not allowed by default so we have to differentiate them by specifying number of parameters should be different for every method data type of parameters should be different suppose first criteria is what Number of parameters should be different suppose sometimes the methods are having same number of parameters the first method having two parameters second method also having two parameters in those cases what we can do is we have to change the data type of parameters okay
normally the number of parameters should be different for every method in case if number of parameters are same then the data type of parameters should be different Okay suppose the data type of parameters also same then order of parameters should be different okay so first method taking int and integer second parameters taking some double and double or double and integer like that so order of parameter should be different so these are the four rules we have to follow when you overload methods methods name should be same number of parameters should be different if number of
parameters are Same then data type of parameter should be different if the data type of parameters are same then order of parameters should be different so these are the four rules applicable when you overload methods okay but before that we need to understand why overloading is required what is the use of this particular Concept in object oriented programming so let me tell you the scenario and based on that we can understand why Overloading is required whether it is useful or not okay so let us try to understand with simple scenario let us say uh I
have one scenario let us say I want to add multiple numbers that's my requirement it can be two numbers or it can be three numbers or four number five numbers okay whatever numbers I will provide it should add so for that I want to create uh some methods in the class So for example I'm creating one method called add uh two numbers so I created one method which will take two parameters and I'll create another method called add three numbers I'll create another method here I have to pass three numbers and add four I create
another method for adding four numbers so now I have created three different methods for adding two numbers one method adding three methods adding four methods sorry Having two numbers having three numberb numbers adding four numbers so three different methods I created for two numbers three numbers and four numbers now if you want to access these methods through the object we will access them differently through object right object name do add two add three add four like this suppose if you want to add two numbers then what you will do add two of two numbers you
will pass if you want to add three numbers then I Will call add three numbers method and I will pass three numbers and if you want to add four numbers then I will call another method and here I will pass four parameters and this is the definition and this is a usage now as a user don't forget about programming classes and everything as a user if you understand this but scenario I have three methods for adding two numbers three numbers four four numbers numb three different methods are there And if you want to add two
numbers you have to call this method you have to remember this if you want to uh add three numbers then you have to call another method again this method also you have to remember if you want to add four numbers then you have to call another method again you have to remember this method right this is one scenario let me tell you another scenario again same uh same thing but here the method names are same Add add add here you can pass two parameters it will accept two parameters here it will accept three parameters here
it will accept four parameters the method name is same now as a user point of view if you want to use this method if you want to add two numbers then I will pass two numbers in add method if you want to add three numbers I can pass three numbers in the same method and if you want to add four Numbers then I can pass four numbers to the same method so this is another scenario if you compare these scenarios in the first scenario in the second scenario implementation wise there is no Advantage so here
we have created three methods here also we have created three methods effort is same development effort is same here the first adding two numbers implementation is there here adding three numbers implementation here adding four numbers Implementation same thing here also we need to you add two numbers implementation three numbers implementation and here implementation of four numbers the development effort is exactly the same no change no difference no Advantage but when you come to the user point of view here in the first case we have to remember names for every method right if you want to
add two numbers we have to call one method if you want to add three Number then I have to call another method if you have to add three four number then I have to call another method so here we have to remember all the methods okay and here we don't need to remember any methods just one method is doing everything so as a user suppose user doesn't see any code and implementation part okay and these are the two scenarios are there in front of you so which one you will prefer first One or second one
so in the second one you have only one method you feel your you're having only one method but it is adding two numbers adding three numbers adding four numbers same method is doing all three things but here multiple methods are doing multiple things okay in this particular case which one you will prefer the first one or second one let's say this is my first one this is my second one always we go with the second one is easiest one right because In the second scenario we will remember only one method and it is doing everything
two numers three numb four number everything right so according to the user perspective the second one is the easiest thing okay but velopment point of view effort is same here we have to implement three methods here also we have to implement three methods but the only change is here we put different names but here we put same names okay and this is the main Advantage of using polym so one thing can have many forms one method can add two numbers same method we can add three numbers same method we add four numbers as a user
perspective I'm telling but in the reality we have different methods for different different operations for adding two numbers one method is there for adding three numbers another method for adding four number another method is there implementation is different but according to the user what I will feel Is every time I'm calling only one method taking two parameters it is working three parameters is working four parameters also it is working so as a user point of view we think this is more preferable this we can achieve using polymorphism or overloading concept so we can create multiple
methods with the same name but the number of parameters should be different or data type of parameters should be different or order of parameters should be different so by Following these rules we will Implement these methods as part of the class this is called overloading method overloading concept okay let us try to implement this practic most important concept listen this very very carefully now let's go to eclipse and create new package for today is day 12th now so in this I'm creating a new class I will name it As Adder I'm not taking main method
so in this I will just create uh only variables and methods okay so this is the class in this I will take uh two numbers inter uh a is 10 and B is equal to 20 now I'm creating one method void sum void sum I'm not taking any parameters observe this so inside this I will directly add two numbers a plus b Because A and B already hardcoded these two values so whatever values I already hardcoded by default assign so I'm just adding those two numbers so this method is not taking any parameters okay this
is the first method I created let's give some number to this first method now I'm creating second method void sum observe here when I create a second method with the same name again same thing return type is same method name is Same no parameters here also no parameter exactly same right so it is giving a duplication if I put cursor here it is saying duplicate method some in type Adder it is saying it is a duplicated method same structure we should not create another method with the same structure so what I will do is what
is the first rule number of parameters should be different if number of parameters are same then data type of parameters should be different if the Data type is parameter same then order of parameters should be different let's see the first point number of parameters should be different so here no parameters but here I will take two parameters both are integer inex comma into y two parameters I'm taking so once you take two parameters and then here I will add x + y these two numbers I'm taking as parameters and then I'm directly adding those two
numbers whatever numbers we are getting as an Input here as a parameters we're just performing sum of those two numbers so this is one method okay now is there any difference between these two methods yes the definition is totally different here no parameters here there are two parameters so it is accepted they are not duplicated now so how we have differentiated these two methods based upon the number of parameters so here there are no Parameters but here there are two parameters there is a difference the first point is applied number of parameters should be different
so for these two methods number of parameters are different so it is accepted to create two methods suppose if the number of parameters are same then what is the next rule data type of parameters should be different okay for example if I write The same method again one more time it is duplicated again it is duplicated why the method name is same number of parameters also exactly the same so now it is duplicated so if it is a duplicated how can we differentiate these two methods the number of parameters are same here so we need
to differentiate with the data type of parameters so what we can do now we can put one variable is integer the other One is we can make it as a double okay now it is different so here we are adding x + y so in method overloading the first point the number of parameters should be different so if I look at first two methods here no parameters here two parameters so these are different and if you compare two and three two and three methods the number of parameters are same the number of Parameters here two
parameters here also two parameters but the data type of parameters will be different so here there are both are integer but here one is integer other one is double so there is a difference so if the number of parameters are same then the data type of parameters should be different okay now if the data type of parameters are same for example let's say I'm creating the same method one more Time now observe here this is another method we should not consider the return type in overloading okay so now I look at this if you compare
these two three and four three and four number of parameters are same and data type of parameters also same so they are duplicated so if the number of parameters are same then what we should do if the data type of parameters are Same then what we should do we should change the order of parameters so what we can put now this double you can put here or you can put integer like this so or else you can make uh in or you can say double X and into y so like this you can specify okay
now the data type of parameters are Different so number of parameters are different data type of parameters are different order of parameters are different okay these are the three points are applicable if number of parameters are if number of if number of parameters are same then data type should be different if the data type of parameters are same then order of parameters should be different so three points are applicable or not yes and let's create any number of methods let Me create another method with three parameters wide sum in a comma in B comma in
c three parameters I'm taking and now I'm adding A+ B+ C like this so you can create any number of methods like this with the same name but you have to make sure the definition should be different this is called definition this part is called definition definition of the method and the body is called implementation okay the implementation Is can be anything but the definition should be different see now we can just look at these methods each and every method is unique they are totally different you can see this is no parameters here two parameters
both are integers here also two parameters but one is integer the other one is double here this is also two parameters but one is double another one is integer there is a difference between these two now the third method is taking three Parameters all three are integer parameters like this you can create n number of methods with the different combinations different number of parameters different type of data different order of parameters you can specify and you can create different combinations of methods this concept is called method overloading okay now we have declared some of the
methods let us try to access them through the object again I will Create another main method so from the main method we'll create an object through the object we can access all these methods so I'll name it as Adder main here I'm taking main method okay now just observe so these are the methods we have implemented I've given numbering also this first method second third fourth and this is my fifth method so totally five methods we created now let us try to use so this is the development part Now let us think like a user
so you have a main method now your objective is you want to access the methods from this Adder class so how can we access first we have to create an object of Adder class and through that object we can access everything so let us create an object of Adder class Adder at obj equal to new add okay so now I created one object of Adder class so with this object we can access all the meths so now you can just Tell me based on the parameters it will go and call particular method see add object
dot sum I'm not passing any par if I look at this also when you say dot just type sum here see how many flavors of some method it is giving here totally five methods so this is called polymorphism overloading concept see as soon as you say sum here it is suggesting five different methods there are actually five different methods we created all method names are exactly the Same but they are different forms one is no parameters one is taking two parameters another one is taking three parameters one is taking intable another one is taking double
and integer the different flavors which we have created so now it is automatically suggesting everything which method you want to take so depends upon your data you have to choose the right combination you can see no parameters one is double and integer integer and double two integer Parameters three integer parameters so you can see three multiple methods are Auto suggesting here now I'm taking the first method no parameters so when you call this which method will be called when I call the some method which one will be invoked obviously the first one okay because here
we not passing any parameters so at the run time it will check is there any method which is not taking any parameters yes there is one Method is available so obviously this method will be called so what this method will do because A and B values we already hardcoded so it will just perform those some of sum of two numbers a plus b so when you run this it will just print 10 + 20 which is 30 so this will call my first method okay now let's see another method I'm going to call add same
object get same method as a user I will always feel I'm calling only one method but in Reality there are multiple methods we have implemented so now I'm passing uh let's say two integers let's say 10 comma 20 or I can say 100 comma 200 so now how it will pick the right method first it will check number of parameters two parameters we are passing now in this is there any method which is taking two parameters yes there are three methods are there which are taking two parameters right so the second third and Fourth these
three methods are taking two parameters okay it filtered three methods so number of parameters are same now what it will check what is the data type which we have passed both are integers now in these three methods it will check is there any method which is taking both the integers yes now the second method is taking both are integers so obviously it will call the second method second method so here 100 will be stored in X and 200 will be Stored in y and then it will perform some of those two numbers 100 + 200
when I execute it will show you 300 similarly let's take another method add obj do sum of this time I'm taking uh this one double integer so first I'm passing a decimal number second one I'm passing integer now which method will be called first one is decimal second one is integer obviously the fourth one will be called okay the fourth one first Parameter is double decimal second parameter is integer there is only one method which is available with this combination so obviously this will call fourth method okay now it is performing 10.5 + 20 which
is 30.5 and uh suppose if I pass arob BJ dot sum of su of and here I'm passing integer and then decimal number first One is integer the second one is decimal number now which method will be called first one is integer second one is decimal third method will be called so this is exactly matched so this will call third method 10 comma 15.5 so 25.5 and uh suppose I'm calling one last method add obj dot sum of now this time I will pass three parameters all three are integer 10 comma 20 comma 30 three
integer Parameters so when I run this we have only one method which is taking three parameters all three are integers so this is got executed so this is executed F method okay suppose uh I call like this add obj do sum of okay 10.5 comma uh 20.5 comma 15.0 Now tell me which method will be called so when you all three are decimal Numbers all three are decimal numbers which one will be called okay so this will give an error when you put the cursor what it is saying the method sum of in and double
so this method is available int and double combination with the two parameters are available but not this type of method is not available all three doubles all three parameters are decimals so this type of definition is not created inside the class it is not Found this combination is not found in this class so this is the reason it will give you an error here can see here the method some int and double in the type Adder is not applicable for the arguments of three so basically we are passing decimal number at the beginning right decimals
so it is basically checking is there any method is having decimal number it found only one method this is the one so it is taking only two parameters but here we're passing three Parameters along with the double so it is not matching so this is invalid because that definition is not there in our class okay so this is how we can Implement method overloading concept so method overloading means you can create multiple methods with the same name but the number of parameters should be different different if number of parameters are same the data type of
Parameters should be different if the data type is same then order of parameters should be different so by following these rules we can achieve method overloading concept so method overloading is an example for polymorphism is this clear everyone what is polymorphism and Method overloading so polymorphism is a theory concept and that we can achieve using overloading same thing we can apply for Constructor also we can also have Constructor overloading now I will show you one more example for Constructor overloading so very very important in the coming sessions we will also discuss about overriding which will
comes under inheritance so that time again I will compare these and I will show you the differences okay so void some of in X in y is equal to why sum of in yes both are equal why because the data type number of parameters are same data type is of Parameters also same right obviously the same methods are exactly the same just you have change variable names that will not make any difference okay so if just changing the variable names will not make any difference so here X and Y you put right if method you
can create another method like this same method but just change only parameter names you can say A and B so that will not make any difference because the data type of parameters are Same order of parameters are same number of parameters also same these are the three things which will be considered the variable names we will not consider okay the number of parameters the data type of parameters order of parameters exactly the same so this is not overloading so can method overloading have different written types yes written type also we don't consider okay so this
can be void this can be Integer okay this can be any other type of return type so we don't consider return type we will consider only parameters number of parameters data type of parameters order of parameters okay so we don't consider void as part of overloading we will not consider the return type of uh overloading let me just give you a simple example you'll understand so I'm just creating this method again one more time okay same method no parameters no Parameters but instead of print Ln I'm just trying to return return this now what could
be the return type here what would be the return type here integer is a return type now we can see these two methods are duplicated still even though if you change the written type they are duplicated right even though I have changed the written type of this method it's duplicated still it is duplicated what does it mean in overloading we Don't consider the written type we will consider only the parameters we will consider only the parameters so now you understood the return type also if it change it will not make any difference so now again
even though if you change the written type these two methods are exactly same exactly the same according to overloading concept okay according to overloading concept three rules are applicable number of method number of parameters should be Different data type of parameters should be different order of parameters should be different but if you look at here all three are same here we're not talking about written type we're talking only about parameters so if you look at the parameter wise everything is same so return type will not make any difference in overloading clear everyone so far and
rest of the experiments you guys can explore okay if you do some changes According to your requirement you do some changes and see effect how they are exactly working why showing error because they are D see I create another method with this integer return return type okay I'm just returning instead of printing a plus b just Len very carefully so here guys can you guess why it is giving an error why it is giving error here it is same type same duplicated so Same method we created again right I already told you the return type
is not considerable we should not consider return type in overloading so if you keep return type aside look at the only methods method wise they are same or not the definition is same or not yes that's the reason is a duplic that's the reason it is giving error clearly saying duplicate method sum in type Adder is duplicated so that's the reason it is giving an error if it just Change only return type that will not remove the duplication okay return type will not be considered only based on parameters number of parameters order of parameters data
type of parameters we will differentiate we will create a different methods okay so remember this point so this is all about method over loading okay so what if if you select the sum of Intex and Double Y but pass Intex 10 and y equal to 20 okay so let Us assume this guys so you have implemented one method called like this some of you taking inex what is integer parameter the second one is double parameter called y so this is your implement methods now at the time of calling at the time of calling you are
passing some of at the time of calling you're are passing two integers 10 comma 20 so will it work or not will it call this method or not yes or no here in the method the Parameter type is what one is integer second one is double here what type of parameter we are passing both are integers so how it will match it will not match okay it will not able to call so if it is a decimal number like this then it will match first one is integer second one is double right if both are
integers this method will not call so it will take another type of method they should have both are integers in X comma in y this Method will be called if you pass two integer ameters only this will call this will not call because why the data types are different here both are integers here first one is integer but the second one is different right so according to the values which we have passed this method is not suitable the second method one is suitable for that you understood now so this is overloading concept first level it
will check the number of parameters second Level it will check the data type of parameters third third level it will check order of parameters okay Rahul understood so this is how it will work so data type is very very important you Define integer and double here but you are passing both are integers how come it will match it cannot okay it will check is there any other method which is taking both Integers so there is another method which is taking two integer value so this will execute according to your values okay and for example here
I'm passing 10.5 double and integer now which will will call which method will be call in these two methods you guys can tell me I have method with this and I have another method both integers and I'm passing first one is a decimal second one is Integer no one because with this this combination there is no method in this first one is a decimal second one is integer but here first one is integer second one is double it will not match right suppose if you have a method like this sum of first one is double
second one is integer then it will match so this combination will match the third method okay so like this it will go and pick the right method If second method is not there will it allow like 20.0 no see decimal is a decimal here integer is integer you have to carefully use it okay suppose here integer and integer y so you should not pass decimal even do zero also is not accepted even if you put dot zero also that is a decimal number right so it will strictly follow the data data type guys okay it
will strictly follow the data type so normally as per the data type if You create double variable suppose I'm creating some double variable okay here I can store 20 this is right but how it will store just like a 20.0 so this is the actual integer value I'm able to store in the double variable this is allowed but internally how it will show when you try to print a value of a it will it will give you 20.0 by do0 by default it is added but here it will not like that we pass integer that
cannot be stored in the double it's not Allowed in overloading it is not applicable integer means strictly we have to pass integer double means we strictly we have to pass a double okay yes now this is all about method over loading now let us see con Constructor overloading so in the last class we have understood what is Constructor what are the different what are the differences between method and Constructor right so based on that uh let us try to achieve Constructor Overloading for that I'm creating another example let's create a new class and Constructor overloading
so I'm just creating one more class called box simple box and in this class I'm going to create some Constructors so what is the first rule of Constructor what's the first rule when you create a Constructor we have to follow Constructor name should be same As a class name whatever name you have given to the class the Constructor name should be same okay so let's create one Constructor and before that let me take and what is the purpose of Constructor the last class we discussed what is the main purpose of Constructor what is the purpose
what is the goal of Constructor yes just to assign the data to the variables nothing else okay Constructor don't do anything just for Assignment so here I'll take uh three different variables all the decimals double width height every box is having WID height depth and everything right so I'm just taking three measurements of the Box width height width height and depth so three parameters I'm taking all three are decimals now to assign the data into these variables I'm creating one Constructor Constructor name is what box and this particular Constructor is a default Constructor because I'm
not passing any data into it so what this Constructor will do is I'm by default I will assign zero to every variable so what I will do is I can say width equal to zero height also Zer okay and depth also zero so I'm assigning zero as an initial value for every variable so this is the default Constructor and instead of writing three statements okay we can also do like this because all values are same right so width equal to p8 equal to depth equal to zero single statement also we can write like this instead
of writing three variables because the values are exactly the same right for all three variables the values are same so single statement you can write so when I invoke this Constructor the zero is a default value For all three variables now I'm going to create another Constructor box now what will happen what is why error is giving here because it is a duplicated there is no difference for one first Constructor and the second one right both are same duplicated now same rules are applicable here three rules what are the three rules in overloading the number
of parameters should be Different data type of parameters should be different order of parameters should be different at least one rule should applicable so in this I'm taking three parameters okay and if you don't take any parameters what type of Constructor it is it is default Constructor okay now in the second one I'm taking uh three parameters let's call this is my first Constructor and this is my second one in this I'm taking three Parameters uh double all are decimals double w width and double height H and double depth D so three parameters I'm taking
again these three values I'm reassigning to the class variables so width equal to W height equal to H depth equal to T so now I'm taking three parameters and again I'm assigning the data into The class VAR so this is another Constructor so now there is is there any difference between first two and second one yes first Constructor is not taking any parameters just assigning the zero to every parameter every variable now in the second Constructor we are expecting three parameters and again assigning the data into three variables this is another type of Constructor so
what is the difference between these two no parameters but here there are some Parameters three parameters okay now I'm creating one more Constructor box this time I'm taking only one parameter double length one parameter and same value I'm assigning to all the variables so let us take the same thing and here whatever value we are getting here that I'm going to assign that means what width height depth all values are exactly the same okay so I'm taking one parameter and same value I'm assigning to all Three variables this is another Constructor which is taking one
parameter now three different Constructors I have created first Constructor no value no parameters second Constructor three parameters all are double third Constructor only one single parameter which is double so now all three Constructors are are totally different so inside the Constructor we should not perform any operations it is just for initializing the data into the Variable or assigning the data into the variables now to perform some operations what we can do we can create some other method so I want to find volume of the box I want to calculate the Val volume of the Box
based upon the values of width height and depth so I can create one user defined method okay and that will return return a double volume this my user defin method and inside this I want to find volume of the box so how can we find just we have to multiply all These three variables and width into and height into depth that will give you volume so that I will again return return that I will return return it I'm not printing I'm just returning it width into height into depth I'm returning it so this method will
calculate uh volume of the Box okay now return type is what double so when you multiply all three variables it will return the decimal number again so we are returning type is decimal so this is Additional user defined method which we created to calculate volume of the Box based on the values which we provided by using con Constructors okay this is the implementation of box class three Constructors I have written now let us see how to use it I create another class main main class I'll name it as box main take this main method click
on finish okay now just observe how to invoke The Constructor how to invoke The Constructor how Constructor will be called yes as soon as you created an object immediately Constructor will be invoked you know need to call Constructor separately using object just you create an object Constructor will automatically invoke okay now just observe I have three Constructors this time okay so I'm creating an object of box class box b equal to new box okay so box box b equal to new box B Is a object of the Box class now observe here when I leave
the statement like this which Constructor will be invoke just tell me first one second one and third one there are three Constructors are there which one will be invoked yes because we are not passing any parameters here so by default it will execute default Constructor okay in the default Constructor we are asscending zero to Every variable right so now I'm just calling volume method the last method volume this will calculate volume of the box so what I will do is I can say B dot volume so this method will return the volume return means we
have to hold that return return value in a variable or we can directly print it so here I'm printing so B do volume will return some value for us the return value you can can hold it in another variable or you can directly put inside the print l so That it will directly print now what is an output You're Expecting here zero why because the default Constructor so this will invoke the first Constructor in the first Constructor the values are zero so 0 into 0 into 0 Z only right so when you call volume method
it will perform 0 0 Z because it is a decimal number we return so obviously do Zero by default it is added okay now if you want to invoke This Constructor how can we invoke the same object we have to pass three parameters okay by using same object we cannot invoke all types of Constructors with one object we can invoke only one type of Constructor okay now this time I will pass three parameters 10.5 15.5 5.0 okay now three parameters all three are decimals doubles now which one will Be executed second one second Constructor will
automatically executed so the values will be assigned so here W will receive 10.5 H will receive 15.5 D will receive 5.0 so again reassigning all the values to the class variables width height and depth and after after that we are calling volume method it will perform volume of the Box yes now we can see this is the output we are getting this is the value of 10.5 into 15.5 into 5.0 right now let us pass uh box b equal to new box of I'm passing single value let's say 10.5 single value so so this will
invoke the third Constructor because in third Constructor we're taking only one input which is decimal number so the same value will be assigned to the all three variables so now how it will be calculated 10.5 into 10.5 into 10.5 so when I execute this will give you output like this this is the volume Of the Box all three variables are having same value 10.5 into 10.5 into 10.5 okay so this is how we can achieve Constructor overloading we can create multiple Constructors also but again same rules are applicable number of parameters should be different if
number of parameters are same then data type of parameters should be different if data type of parameters are same then order of parameters should be different so by following these three rules we can Achieve method overloading or Constructor w overloading so is this clear everyone please confirm in the chat box overloading through overloading we can achieve polymorphism polymorphism means one thing can have many forms very simple understanding so this much is enough polymorphism concept is over okay now let us move on to the next next concept so far we have completed three Concepts from objectoriented
programming one is class second one is object now polymorphism so three concepts we have successfully done so whenever I say overloading you should remember overloading is related to polymorphism whenever I say polymorphism you should remember overing so these two concepts are you have to remember together okay now the next concept of objectoriented programming is encapsulation Encapsulation okay so what is uh encapsulation encapsulation means the wrapping up of data and methods into single unit single unit in the sense class data in the sense variables and we already know methods this is a definition official definition from
java flapping up of data data means variables and methods into single unit single unit in the sense class so what does it mean is we will have all the variables methods in one single class that means We are already following it but slightly difference so far we are creating variables and methods in the class only right the class contains the variables and methods and what is that addition meaning of this wrapping up of data and methods into single unit which is called encapsulation feature so let us try to understand why this feature is available and
what is the importance of this feature what is the use of encapsulation capsule means what which Contains a medicine right what is a capsule capsule includes a medicine can we see what exactly there in the capsule can we see the medicine in the capsule inside the capsule no it is hidden part right you can just see outside layer but we can't see what is there inside this medicine right similarly in encapsulation we will create a class the class contains the variables and methods but they are not visible to outside classes it is more over like
a security Kind of a feature so what we need to do is we will create one class like this let us say this is Class A and in this we will create multiple variables along with some methods this is as per the definition right the class contains a variables and methods so we create a class which contains a variables and methods so normally how we are accessing these variables and methods through the object right in another class let's say we have another Class called a b in this class we will create create an object of
Class A and through this object we are able to access everything variables methods everything so once you created an object of Class A through the object we are able to access everything directly object name do variable name object name do method name everything we are able to access as soon as you created an object this is the default feature which We have already seen now what encapsulation says encapsulation we put every variables and methods in the single unit single class but we don't give access on this variables outside that means through this object we don't
directly access the variables we cannot directly operate the variables instead we will just call these methods and these methods will operate the variables that means means we are Not directly we are not directly accessing the variables but only through methods we are able to operate the variables okay this is called encapsulation so why we need to implement is just kind of a security me simple example I'll tell you you have some bank account let's say this is your bank and you went to the bank and you have account with this bank and there is a
customer okay some guy is sitting here when you reach your bank okay if you ask Your account details like what is your account balance everything will they provide directly will they provide you directly let's say you have details all banking details they're having with them okay so if you directly reach out to the bank if you ask provide you all the details can they give directly no they will first ask for some identity right so your identity details they will ask your other number p number and so on so once They ask once you provided
these details they will cross check right and accordingly they will read the data and they will provide it so they don't directly give access on your details so there is a mediator is there so you you provide some authentication information then only he this guy will provide the information to you you are not directly accessing it similarly in encapsulation whenever you create an object through this object we cannot directly access The variables only through methods we will able to operate the variables we can assign the data into the variables only through methods we can operate
the variables only through methods we can do operations on the variables only through methods but we cannot directly access the variables anywhere else through the object so that is a feature of encapsulation wrapping up of data and Method into sing single unit and we can Operate variables only through methods not directly okay this is called encapsulation how can we achieve this so to achieve this what we have to do is by default we will able to access all the variables through the object by default we able to access all the variables through object normally but
if we don't want to access what we can do is we have to make all the variables are private this is the first point you have to make Variables you have to create a variables with a private access modifier in front of every variable you have to keep private keyword okay whenever you make the variables of private those variables can access only within the class only within the class means the methods can access but outside of the class you cannot access because we make them as a private means more secure so as soon as you
make them as a private through Object also we cannot directly access but how can you operate the variables how can we assign the data into the variables we have to create methods for this variable for assigning the data and reading the data so if you create any variable what are the basic operations we do on the variable we can assign some data and we can read the data from the variable right these are only two things we do normally assigning the data reading the Data so how many variables we created here for every variable we
create two methods one is for assigning the data into variable other one is to read the data from the variable that means if you have a 10 variables you have to create 20 methods in the class for every variable there are two methods one is for set setting the value into the variable other method is for getting the value from the variable so the methods which we created to set the values into The variable we call them as a sets and the methods which we created to read the data from the variables we call this
a Getters and there are two kinds of methods we have to create Setters will set the data into variables Getters will get the data from the variables for every variable we have to create two separate methods and by using those methods we will try to operate the variables but not directly this is called encapsulation This is just a kind of a security kind of a feature but uh in development prospective is more useful but automation testing point of view is not that much useful feature okay so the guys understood the concept what is encapsulation first
of all encapsulation means raping up of variables and methods into single unit and we don't directly access the variables we operate the variables only through methods methods can access the Variables okay that's the concept how can we achieve this two different things first thing we have to make all variables are private second thing is what for every variable we have to create two methods one is Setter method other one is getter method that's it okay let us TR to implement this very easy concept just try to implement this so I will write all the points
here So we have written the definition so what is encapsulation the first point all variables should be private whenever you create a class all variables we must make it as a private there the first thing and for every variable there are two methods should be created one is get method other was set method and variables can be operated only through methods okay so should we write two methods for all variable that will not be encapsulation you can create no Problem in that but that's not comes under encapsulation but there is an disadvantage in this I
will tell you what is a disadvantage if you create one or two methods for all the variables you can create no problem in that but that is not encapsulation see what is the problem in that I will tell you I'll tell you what is a problem in that see I'm taking same example bank Account example so let say this is your bank okay you have account number balance and some other details some personal information here there is a banker these are details they have with them and now you reached your bank you approach your bank
people and what you are provided you are just provided your account number and based on account number they will fetch the all the data but they won't provide all the data at a time suppose if you need only balance Amount only balance then they will tell only balance to you okay and whatever information you have asked only that information they will provide you so if you create only one method let's say you created only one get method for all other variables this method will get you all the information at a time all the variables but
you want only account number you want only balance okay when we get it specifically that variable you Can't get it right so if you create a separate method let's say get balance get account number if I create multiple methods for every variable then you can you will able to get only that particular data whenever you want if you write one single method for all the variables what will happen when you call that method you will able to read all the data that's not encapsulation okay that's that is the reason we have to create two methods
for Every variable one is for setting the data other is for getting the data so then only you will operate individual variables separately okay if you create one method for assigning the data into all variables and reading the data into all variable you can create but that is not encapsulation feature what encapsulation says is every variable is a private and every individual variable should have a two methods one is Setter and getter Method and through those methods you will operate variables individually not combinedly okay so just understand the concept here now let us try to
implement this practically so these are the points which we have to remember while implementing encapsulation all variables should be private for every variable there should be two method gets and sets and variables we should operate only through gets and seters not through the object that's the main purpose we should Should not able to access the variables but we have to provide a way to operate the variables what is that way is through methods okay I hope everybody's understand the concept theoretically now we'll implement this practically encapsulation now create a new class and I'll name it
as account new class my class name is account I'm not taking Main method I'm just creating a new class in encapsulation if you want to apply encapsulation so how can we create a class so as per the definition we should have some variables and methods let's create a variables I'm creating in account number is one variable Stringer account name and double uh amount so three variables I'm taking uh now I'm not making any private okay just have normal variables and see uh later I Will apply private keyboard so these are the variables I have created
okay for example let me create main method also new class and I name it as account main class I'm taking main method here okay just observe here these are the three variables which we created now in the main method I'm creating an object of account class observe this account ACC equal to new account I created an Object now you guys can tell me by using this object can I access these three variables or not at present can I access or not yes how can we simple ACC dot ACC number you're able to access right you
can assign the account number ACC do name you can assign the name also this is a direct ACC do you can apply you can assign amount you can assign some amount so we able to access directly this is not in capsulation so What is the first rule we have to do is we have to make them as a private so we have to add private keyword in front of the variable like this for every variable we have to make as a private now observe very carefully as soon as you make them as a private here
you can't access it see it is started giving an error what it is saying the field account number is not visible and the field the name is not visible so the amount field is not Visible why they have not visible because we make them as a private so as soon as you make them as a private we cannot directly access this is not possible okay first two is applied but then how can we operate these variables only through methods so we have to create methods here for every variable two methods we have to create as
I said one is getter one is Senter so let us create so for account number I'm going to create one Method void okay and set account number this is one method I'm creating set account number here I'm taking account number as a parameter also I say in ACC number and that I'm going to store actual ACC number so here this is ACC number this is account number or you can say AC number I'm giving different names and here in account Number I'm storing this AC number is it correct or not yes so I'm taking one
parameter and assigning the value into the account number so this method will assign the data into the account number okay here I will tell you one more thing this AC number is a local variable this ACC number is a class variable so here the local value I'm ass into the class variable can we use same name Here like this can we use same name class name and local variable name can I use same class name and local variable name okay so let us try to use for now so I'm using the same name here and
assigning okay nothing wrong in this I will tell you what is the problem with this so this is one method which we created for set the data into account number similarly to get the data from Account number I'm creating another method vo get ACC number this method is for getting the value getting means return I want want to return it return ACC number and what could be the return type now integer is my return return type so for this particular variable I have created two methods one is for setting the value into the variable other
is for getting the value from the variable okay now these methods we can Access right in encapsulation these methods we can access so now come to the main class observe carefully here through the object DOT first I will call set account number method set account number so here I'm passing some account number so then what happens this method will call set account number and receive this value into the account number variable and assign the data so we are able to operate this variable the next after Assignment I want to get the value from account number
ACC do get account number so account number will return the value here returning means what we have to store that return return value in a variable or we can directly put inside the print statement so this is the way we will operate so here we are not directly operating the variables we are not directly accessing the variables we cannot why because we make them as a private in addition for every variable We have to create like this two methods are required one is for setting the other is for getting the value so here we set
the value in the variable through method we also get the value from the variable through method this is called encapsulation okay now let me run the code and observe the output see it is giving zero we passed one1 but it is giving zero why it is giving zero I'll tell you reason so when you say one when you pass 101 here it is received correctly 101 when you're trying to assign the data here the class variable is what account number local variable is also account number when I say account number equal to account number we
know which account number is a class variable which account number is a local variable but this method don't know that this method will confuse which one is a class variable which one is a local variable because both are same right we are given Same name to the class variable and local variable so the set account number method is confusing which one is a class variable which one is a local variable that is the reason it is assigned default value which is zero the default value of integer variable is zero that is a reason it is
giving account number as a zero why it is happened because we use the same name class name and a local variable name but still I want to use same name How can we differentiate this explicitly I should say this is a class variable and this is a local variable so to differentiate these two variables we can use this keyword this so here we have to specify this dot account number so whenever you see this keyword that is always representing the class okay remember this point so always this keyword is representing class whenever you use it
so when you said this do account number yes now this Method will know this account number is belongs to the class and other account number is belongs to the local varable okay now you understood where exactly we have to use this keyword same thing is applicable for method also Constructor also okay where whenever you are using class variable name and local variable name is same then you have to differentiate them by using this keyword okay now if you get if you run this you will get the right Output one 1 so we have achieved encapsulation
so whatever variables we created for every variable we have to create two methods one is Setter other one is getter okay understood everyone now I'll show you something else here you understood this key also right what is the purpose of this keyword here yes now observe very carefully will there be necessarily to access all private variables in all Classes not necessary if there is a requirement you should able to access otherwise no don't access yes now let me tell you something else here these are the user different methods right we can give any name no
problem but the method name should be more meaningful if you read that method name you have to know okay this method is for setting the data this method is for getting the data you have to easily recognize so accordingly you have to Provide the name to this method okay I will show you something very easiest to think here I have only three variables so I can easily create for every variable two methods means two into three six methods I can easily create manually suppose you have a 10 variables or 20 variables it is very difficult
to create so many methods right it's very difficult to create many methods for every variable if you want To 10 variables are there 10 into two 20 methods you have to create manually it is very tedious task right so what we can do is Eclipse is provided some shortcuts eclipse is provided some shortcuts by using those shortcuts by using using those shortcuts we can easily include all Setters and Getters for all variables simply I Will Show You observe very carefully I'm just deleting this okay now automatically I want to add all Sets and gets what
you can do is put the cursor here once you created all the variables just put the cursor here and there is a option called Source menu you can see Source inside the source you can see one option called generate sets and gets this is the option generate sets and gets you can just click on this option and you will get one window here it will show you all the variables currently I have only three variables so it is showing three Variables and you can select here for which variable you want to create a Setter and
getter methods you can specifically write or I want to generate sets and gets for all the variables then click on select all it will select everything all the variables and simply you can say generate that's it see how it is useful it is generated set us and get us for all the variables if I look at here see this is For getter method for account number get account number and return return account number and this is for what set account number it is taking account number as a parameter and assigning account number to the class
variable and this is for getter for name this is Setter for name this is get method for amount this is set method for amount very simple right compared to the manually written all the methods it is very very simple way of inserting Setters and Gets okay I will show you one more time let me remove everything listen this very carefully guys focusing see now I want to generate set us and gets for these three variables okay simply put the cursor here after creating all your variables put the cursor here then go to Source menu here
you can find that option generate gets and Setters click on this option you will see all the variables here you can suppose if you want to create Setter and get a specific Variable you can select specific variable like this or if you want to generate sets and gets for all the VAR you can click on select all once you selected click on generate click on generate button that's it it is automatically generated all sets and getter methods and Public Access modifier by default it will be added because these methods we can access anywhere everywhere throughout
the project you can access that's the reason Public is by default added okay if you look at here same how we have created earlier exactly similar way it is got created so get account number means when you call this method it will return account number when you call this method set account number by passing account number it will assign the account number same thing for get name set name get amount set amount so this is automatically added all the Getters and Setters for example for this Amount I don't have sets and gets let me remove
this so I have created Getters and sets only for these two variables I don't have getter and setter me for amount variable then what you can do again go to Source generate gets and sets now observe this time it is showing only that particular variable whichever variable not having the setter and getter method it is displaying only that particular variable simply select and Click on generate that's it it is added so if we have so many number of variables this option will be very very very helpful okay clear everyone how to add sets and gets
for the variables again you were asking the basic question we already discussed multiple times are repeated what is the use of these Getters and Setters guys can anyone tell me what is the use of this getter and Setter Methods what is the purpose of creating the getter and Setter methods what is the use of creating Getters and Setter methods yes because we make the variables as a private means we cannot directly access the variables through this object but how can we operate these variables only through methods so through the get method we can get the
value from the variable by using set Method we can set the value into the variable so like this for every variable we have to create one get method one set method only through methods we will able to operate the variables okay when you call when you use this object only we can call these methods through the object but we cannot directly access the variables okay now once you created your variables like sets and gets here we can just call those methods so ACC dot set See here all set methods are displayed so I I want
to set account number account number is what let's say one Z similarly I want to set ACC do set amount let's say I'm passing some amount and then ACC dot set name I'm just trying to set a name so now I have set the data into all the variables by using set up methods now I want to access the data from the variables how can we do it by using get method so get account number so this will return account Number so that I can print directly acc. account number and similarly system. ACC do get
amount and similarly system. ACC do get name right so like this we can assign the data into the variables by using seter methods we can get the data from the variables by calling getter methods but everything we are able to do only through Setters and Getters but not Through variables we are not directly accessing the variables okay but we are accessing it only through methods that's called encapsulation the concept of encapsulation is like that okay now we got the data like this so this is a kind of a security kind of thing okay instead of
accessing the variables directly we will able to operate only through gets and Setters this is concept of Encapsulation okay so what is the first point in encapsulation wrapping up of data and members into single unit which is a cross all variables should private if you're not private we can access directly through object itself that's not in gap station so we have to make them as a private after making them as a private then we can access the variables only through gets and seters for every variable we have to create one get method one set method
and The variables we can operate only through Setters and gets method that is a concept okay so if you don't make them private we can directly access through the object there is no security everywhere you can access the variables but here Our intention is we don't want to access the variables outside of the class only within the class we are able to Access encapsulation we make the variables hidden to the user when I create an object through this object we cannot access that means for this object these variables are hidden only through methods we're able
to operate okay so this is the concept of encapsulation but automation point of view it's not much useful but the development point of view it will be Very very useful for them because they want to maintain more privacy to the variables I told you one example also the banking example When You Reach Out the bank they don't directly give all the details to you you have to provide your authentication accordingly they will fetch the data and whatever details you want only those details they will provide similarly we just call these methods first and these methods
will get the Data from the variables and then provide to the user you understood now so this is the concept of encapsulation this much is enough guys don't go beyond that okay not much important for us but interior prospective you need to have some idea what is encapsulation feature in object oriented programming encapsulation means the wrapping up of data and methods into one single class we make them as a variables Private so that we cannot access anywhere else and we can operate the variables only through Setters and getter methods okay that is a concept of
enolation okay so an inter perspective these topics are most important what is polymorphism how to achieve what is overloading and what is encapsulation and how to achieve it and what are the rules of polymorphism all those things very very important and one Last thing I forgot to tell in overloading concept just observe this one more last op we have discussed about overloading concept so what is overloading one thing can have many forms we can create same name multiple methods with the same Name by changing the number of parameters order of parameters data type of parameters
and so on now my question is can we overload main Method very very important can we overload main method in every class we are creating a main method right main is also one of the method main is also one of the method can we overload it just like other methods let me tell you the question so class so here uh I will name it as something okay overloading main method here I'm taking main method okay now observe here this is just like a Normal method but it's a special type of a method But ultimately it
is a method there are some access modifier some of the keywords are got added public static void and so on but this is also just like a normal method but it is having special type of attribute because execution will start from this main method always jvm is look for this particular syntax and everything but my question is just like other methods can we also overload main Method that is the most important very popular inry question yes the main method also we can overload main method also we can overload how it is possible let me tell
you so this is the main method right so let's keep this main method one side now I'll create another main method voide main see this this is also main method right this is also main method in this main Method okay I will write something let's say I will take one parameter let's say some x one single parameter I'm taking and here I'm just trying to print the de this is one main method create another main method public Main and here I'm taking string parameter one single parameter but a string okay so here I'm trying to
print yes okay another another uh main meod also I'm taking String uh S1 comma string S2 so two string parameters I'm taking so here I'm printing S1 plus S2 concatenation see multiple main methods we can create but is there any difference or not here the main method name is m so this we will not consider this part we won't consider only the parameters we should consider in overloading concept so here integer single parameter string this is also one single parameter but string type so there is a difference in This here two parameters both are strings
and here what about this ARS is just a parameter name just like x s S1 S2 we are using right same thing this is just like a parameter it's not a fixed value you can put anything you can put a also or you can put X also or you can put arcs also anything it is just like a parameter name okay don't think args is a fixed value no it is just an argument and what type of argument it is string array type of argument string Array type of argument okay so we created multiple main
methods but if you want to access these main methods from where we can access through the object where we can create an object inside the actual main method okay in the actual real main method whatever is there here only we can create an object now let us create an object for class overloading main method W equal to new overloading main me so now by using this OV you can access all the main method you can see here there are four main methods that is show first main method is taking one single parameter 100 and passing
now OB do main second main method is taking string so I'm passing string parameter another OV method I passing two strings so here I'm passing the first parameter hello and second parameter is job two parameters and Proc okay like this now all other main methods I'm able to call when I execute it so now we got our output okay so this is how we can also overload main method we can also overload main method guys understood it's very very popular in question okay so in the actual main method why this parameter is a string array
okay why this parameter is a string type of an array because this Main method can take string array as an argument but from where we will pass these parameters through command line command line arguments in Java we have a concept of command line arguments okay when you run this class through command Pro BR not in Eclipse through command bront we can pass these parameters to the main method also after that we can use these parameters inside the main method also Just like other so here we're passing two parameters we using it here we're passing one
parameter we are using it similarly in the main method also we can pass the arguments string array type of arguments only it is allowed means multiple string parameters we can pass and that we can use inside this main method so that's how they have created this main method okay then only the jvm will able to recognize this main method and Execution will start from the main method that's it we don't know exact reason why they have put only string arguments but we can say this main method also can take arguments string parameters it will take
array type of parameters it will take and we can use them inside the main class okay so we can overload main method so we can overload overload is nothing but what you're missing the concept actually overloading is nothing But one thing can have many forms the main method we have created multiple times or not yes that means what the main method is also will be overloaded the same main method I have overloaded method name is same but the parameters number of parameters are different order of parameters are different data type of parameters will be different
so we are able to overload be method also we can also create multiple main methods that's called Overloading we can create multiple main methods okay so this is how we can achieve the overloading so here not we are not going to deal anything with the command line arguments not at all needed so you can just ignore the topic okay so overloading concept is enough so the development point of view they will normally execute these classes outside of the eclipse okay in the form of Jar file so they will create all the Classes bundled with some
jar file and they will run that particular jar file in remote locations like through command prom or through jenin or somewhere else so in case if they want to pass any parameters to the main method they will use this arguments otherwise no need okay so that's it for today's session and you guys can do some one round of two rounds of practice encapsulation polymorphism that is enough so interview Point of view this is very very important but an automation point of view overloading is important but encapsulation is not that much important it's very simple concept
it's a kind of a security and privacy kind of thing we can provide to the variables let us stop here for today's session and tomorrow we will continue