all right so today uh we're going
to start one new topic rapper classes okay so let us understand what is wrapper
glass and how we can use this wrapper glass and where we have to use is wrapper class so normally
in Java we can store data in two different formats so one is the Primitive format the other one
is object format okay so suppose if you take a string right so if you want to just create
a string variable we will say string s equal to uh we can directly put the value like this or
we can also create a string like an object like I can say string is equal to new string of you can
say welcome so this is another way of creating so here it is just like a uh variable but here it
is just like a object because as soon as we use new keyword it will internally create an object so
similarly do we have anything for primitive types for example uh if I create an integer variable
suppose int x equal to 100 and similarly for this particular integer do we have any wrapper
class that means can we create this variable as an object okay for example let's say if you want
to create Intex equal to new in is it possible to put like this that means here what exactly we
are doing is we are converting uh this is a normal primitive type but this is an object but when
you want to create an object normal primitive type we cannot use primitive type we cannot use
so instead we have to use something called wrapper class means for every primitive type there is a
wrapper class is available for example for INT we have a integer wrapper class so that we have to
use here also we have to use something called uh integer this is the wrapper class okay this is a
wrapper class similarly for every primitive type there is a wrapper class is available like for
in there is a integer wrapper class for float we have a float wrapper class for double data type we
have a double wrapper class so for every primitive type there is a rapper classes available so if I
look at here these are are the wrapper classes for every primitive data type there is a wrapper
classes available so what is an advantage of wrapper classes there are two advantages one is
we can create uh a variable with object format okay and the other thing is we can convert the
data into one format to another format okay that if you want to convert the data into one format
another format we can also use rapper classes and if you want to create a data object format also
we can use uh this wrapper classes the wrapper is nothing but just a name the Java people have given
to that because these are the special classes you cannot compare with other built-in classes in Java
for every data type in primitive data type there is a corresponding class is available so only
specifically those classes are called wrapper classes okay string is not a wrapper class string
is a normal class okay but especially whichever primitive data type is there for that whichever
class is available Associated class is available only those classes are called rapper classes
okay so string is not a wrapper class because we don't have a string primitive type we don't have a
string primitive type so we don't have a lowercase string type right so string is a separate class
but we have an integer primitive type is there we have a float primitive type we have a double it
is also primitive type car Boolean these are all primitive types so especially for every primitive
type there is a corresponding class is available in Java only those classes we can call them as
a wrapper classes so what is the use of these wrapper classes mainly is data conversion if
you want to convert the data into one format to another format we can use these wrapper classes
and every wrapper class is having some buil-in methods also whenever I say class the class
contains what the class contains the variables and methods so when I say it is a buil-in class
obviously which contains a buil-in methods means which are already created by Java and we have to
know exactly how to use them so rapper classes are the classes buil-in classes which are available
in Java and for every primitive type there is a corresponding rapper classes available in Java
only for primitive types okay but there are other uh predefined classes also there in Java like a
string class is available okay array list class is available okay hash set classes available
these are all predefined classes but they are not wrapper classes these are all predefined
classes but they are not rapper classes because these classes are not representing any primitive
types so only these are the wrapper classes which are available suppose if you have a shot
also short is a one numeric data type for this also we have a short wrapper class for every
primitive type there is a corresponding rapper class is available in Java that is the first
thing now what is the use of rapper class and where exactly we use this rapper classes so the
wrapper classes contains a buil-in methods as I said those built-in methods we have to use to
convert the data one format to another format but why again we need to convert the data into
one format or another format so let me give you uh some re scenarios for example I have uh prices
let's say there are two items and every item is having some price but that is in string format
let's say string price one equal to it is in string format let's say 150.50 something like this
and I have another string string price to equal to 12050 so let's say this is another string right
so now I have a two strings price one and price two now if you want to add these two prices I want
to find total price so how can we find total price So currently they are in string format right if I
do price one plus price two then what will happen it will basically do concatenation because even
though they are the numbers currently they are in the string format so if you add price one plus
price two it will just just conc concatenate but it will not perform the addition of these
two numbers because they are in the currently in the string format so what we need to do now
we have to convert them into numeric format we have to convert them into numeric format and after
conversion we can add those two numbers so how can we convert into numeric format again we have to
use some method from the built-in classes wrapper classes so by using the wrapper class methods
we can convert data from one format to another format so another example I'll tell you suppose uh
if you take any web application for example just a second uh guys can you see my screen okay just a second okay so if I just look at this normally
in any application uh you can see let's say some registration form so here you have to
enter the name and here you have to enter the email address and here you have to enter the
phone number okay so let's say this is the F so normally uh these are input boxes or we can
also call them as a text boxes so text boxes allow data only in the text format text in the
sense what string format okay only in the string format data is allowed in this Fields so suppose
in our Automation in our automation suppose I want to pass this phone number so normally
phone number what is the type of the phone number number integer numeric types right say 1
2 3 4 5 6 something like this so this is purely numeric format but if the data is in numeric
format we cannot directly pass it into the text field okay in automation I'm soring saying so
whatever data which we have currently it is in integer format or numeric format so if the data
is there in the numeric format the text field or the text box is not allowed the numeric format
directly so the text boxes or input fields are allowed data only in the string format so what
we need to do now we need to convert this into string format and after that we have to pass
it into the field so here we need to convert the data into numeric format to string format
and here we have to convert this data into the string format to numeric format and then we can
perform the addition of two numbers so like this there are n number of requirements and uh we
have to convert the data into one format to another format whenever it is needed so for
that we have to use these rapper classes and these rapper classes having some buil-in methods
so by using those buil-in methods we can convert the data into one format to another format okay
now let me show you some examples let me clear this so wrapper classes so first
let us see the first scenario scenario one suppose I have data uh in string
format I have data in the string format and uh I want to convert this string format data into
int format or decimal format or Boolean format or character format these are the main Boolean
type which we have right so string to in string to double string to Boolean string to character
I want to convert through rapper classes so how we can do it this is one camic second thing is
suppose I have data in Reverse way for example I have data in integer format or double format
or Boolean format or character format all these formats I want to convert into string format
this is another combination so string to other primitive types and all other primitive types
to string type but within this primitive type is not possible okay double to bullion billion to
integer character not possible these are all one single uh category we can say they are primitive
types so all string to primitive types primitive types to string type okay let's see how can
we convert this so to convert this we we have first we will see first combination so we'll take
one string variable which contains some value and then we will convert them into different types
for example let me take the first scenario if I have a string data type I want to convert
this into in format integer format then what is the target type here Target integer type
is a target so we have to use integer weer class integer dot inside this class there is
a static method available which is par in pass int of here we have to pass whichever string
you want to convert to the string we have to pass here string value we have to pass so what
this method will do is this particular method will convert the string value into integer format
so this parent method will return return integer value so here we have to use integ wer class
inside this pass in is a static method which we are calling directly from integer class and
we have to pass the string value so this method will convert string to integer format similarly
suppose I have a string want to convert into decimal format let us say double format I want
to convert so then what is the target type here Target is double so we have to use double wrapper
class double dot the method is pass double and here again we have to pass string value so this
particular method will convert the string value into decimal format so this pass double method
will return return decimal number and suppose I have a string that I want to convert into
Boolean type Boolean string to Boolean type so then we have Boolean rapper class Target is
Boolean so Boolean dot par Boolean and here we have to pass string value okay and the fourth
suppose I have a string and want to convert into character string to character this is not
possible can anyone know why it is not possible string to character string to character why it
is not possible string to character string is what collection of characters but character is
what Single Character right how can we convert multiple characters into Single Character not
possible right so for character it is not possible string to integer string to double string to
Boolean okay string to long string to short string to float all primitive types are supported
except this car okay so now we'll see practically how can we convert this let's go to eclipse and
create new package package name is day [Music] 17 okay now inside this I'm creating a new class
I'll name it as data conversion methods take this main class I'm directly taking main class and
I can directly write the code inside the main method okay so first of all let us see the first
scenario how to convert string to integer string to double string to Bull and string to character
okay first thing string to integer so I'll take take one string variable s equal to okay so here
there is a prerequisite suppose if string s equal to welcome can we convert this into number can
we convert this into number string s equal to welcome string value is welcome can we convert
this into numeric type no this is not possible so even if you try to convert for example I'm
taking integer rapper class integer dot parcent of string I'm passing so this will convert and
after conversion it should return integer right so I'm taking one more integer variable let's say
yes in so this is s is a string variable s is a string variable and integer that par of s what I'm
doing exactly here I'm trying to convert a string to integer format okay so when you try to run this
code this will will throw some exception number format exception it will throw why it is throwing
number format exception because we cannot convert this characters into alphabets into number
so that is a reason okay so that is a reason we cannot convert into number because we have a
characters in this okay so what is a prerequisite now we should have a numeric value inside the
double quotation then only it is possible okay so this we cannot convert cannot convert to int
okay but when we can convert it should have some numeric value let's say 1 2 3 4 5 so this we can
convert string is equal to in the double quotation so even though there are numbers it is currently
is in the string format so we can convert so after that we can print this value can just print yes
in okay so or else let me tell you something so let me take uh like this let's say string S1 equal
to 10.5 or 10 is it equal to 10 so sorry yeah 10 so this is a string format value is 10 which is
number but which is in the string format and now I'm taking another string string S2 equal to 20
this is also in string format so if you want to add these two numbers we can say S1 plus S2 so
it will perform the concatenation but it will not perform the addition of these two numbers
this is a concatenation so if you want to add them first we need to convert them into numeric
format and then we can add so how can we convert this S1 into numeric format rapper class integer
dot pass in of S1 like this we have to pass so now what will happen this method will convert S1 into
integer format same we can do for S2 also integer par okay par of S2 like this okay so if the data
is in string format like this then we can convert them into numeric format and then we can perform
the addition of those two numbers okay now you can see you got a 30 so whenever you have data in the
string format like this if you want to perform any operations it is not possible so you can convert
them into integer format so what is the method we can use integer. par in so string to in integer
rapper class do par in of string value whichever value you want to convert you can pass that value
here so that will convert this into string form in integer format and again this will also convert
into integer format and after that it will perform the Ed of two numbers we can take direct integer
yes you can take direct integer you don't need to convert in that case but what is the case here
if you have data in the string format right then you can convert into number format if you take
the data and number format directly you don't need these rapper classes and all if you take the
data in the number format directly integer format you don't need to use any rid classes you can
directly perform addition of two numbers here the scenario is different if the data is in string
format then how can we perform because in the real scenario it will be very very useful when you do
automation testing it will be very very useful so in automation testing I will show you how it will
be for example let us say in your web application you have some items products and their prices
let's say you have a product one and a product two product three and there are prices are there
like this so prices are there so what is your requirement is you want to read all these prices
and find total price this is your requirement as part of your automation you want to capture all
these prices and you want to perform the total price and you need to compare this price with your
expected price what is the total price so then what you will do you will write the automation
code for reading this data and while reading the data even though are the numeric at the time
of reading you will get the data in the string format so whatever data you are reading from the
web application everything will be in the string format okay so once this data is in string format
you can't perform the total right so you have to convert that into number format numeric format
and then only will perform the total okay so these type of scenarios you can see in the automation
so for that reason we have to know how to convert the data into one format to another format which
is most important but if you're directly writing a program there is no dependency you can directly
specify these two variables as integer format and you can directly perform addition of two numbers
that's not the scenario here the scenario is if the data is in string format how can we convert
into number format but why we need to convert into number format because we can't perform arithmetic
operations on the string format of data okay that is the reason so we have successfully converted
the data from string to integer format similarly we can also convert the data into integer from
integer to decimal format so let me show you this just a second okay let's comment this okay so I'm just commenting this just observe
now the second scenario I want to convert the string to double guys you understood why I'm
doing like this why we need to convert the data from one format to another format you understood
the scenario right yes string to double suppose you have a data in the string format now I want
to convert into decimal format again take the two variables string S1 is equal to this time I
have data in the decimal format like this string S2 equal to okay now I have data in the decimal
format now I have data in the decimal format so if you have data in the decimal format I cannot I can
perform addition of numbers but currently it is in string format numbers are decimal but they are in
string format so now we need to convert them into decimal and then we can add those two numbers so
how can we convert them into decimal so if you want to convert S1 into decimal you can say what
is the rapper class which we have to use double is a rapper class double dot par double of the string
variable so this will convert and similarly I will convert s to also and say double do par double
of S2 okay so this is a value now I'm trying to print this value so now this will convert
the numbers decimal numbers into sorry it will convert string type into decimal numbers and then
it will perform addition of two numbers so this is a one method which we have to convert the string
to double we have to use double dot par double now suppose I have a string I want to convert
that into Boolean variable so what is Boolean Boolean means what what are the values are
accepted in Boolean only two true or false right so if I take a string like this string
s equal to welcome can we convert this into Boolean can we convert this into Boolean we
cannot because they are the characters right we cannot convert this into Boolean suppose
here we have a true this we can convert into Boolean or we can have a false here false then
we can convert into Boolean only if you have a true or false then we can convert that from
string format to Boolean format okay so how can we convert this what is the target type is
Boolean so what is the method Boolean rapper class Boolean dot pass Boolean then we have
to pass yes so now let us print this so this will convert the string to Boolean format so
Boolean do pass Boolean of string so this will convert string to Boolean suppose if you put
something else welcome now run this it is still saying false just a second so by default
it is saying false okay by default other than true if you pass anything here other
than true if you pass anything here that will return false okay that's a default
behavior of this parse Boolean method so other than true if you pass anything that will
return false okay for example let's pass true here so it is returning true
okay let's pass something else it is ring false okay so other than other than true if you pass any string
that will return false okay so this is a Behavior everything is considered as a false
other than true if you pass anything here that will return false so here it will accept only
true or false then only we can convert from string to boan format string to glean format
okay so this will not affect any programming logic so once you're writing the logic you have
to carefully use proper data type proper data so that you have to plan before writing the logic
okay got my point guys so string if you have data in the string format you can convert that
into Boolean format okay now so we have seen these four types and string to character is not
possible okay string to character is not possible by because string contains a multiple characters
because character contains only Single Character so multiple things we cannot convert into one
single so this string to character not possible okay remember this so this is one scenario which
we have seen how to convert the string type to other primitive types int double Boolean and
car is not possible I'm just removing the car from here okay now we'll see the opposite way
suppose I have data in integer double Boolean and character format then how can we convert into
string okay from primitive type to string type how can we convert this is also most important
scenario let us see that so this is is for string to Boolean okay so now we'll see the
opposite way suppose you have a primitive types I want to convert
that into string type primitive type to string type suppose I
have int or I have a double I have so Boolean and I have character so from
these types to finally the target type is what string but string is not a rapper class string
is a not a wrapper class but what is the target type here string so the method also should be
present in the string class itself so how to convert these buil-in types into string format
okay so we will see that suppose I'm taking one integer variable in equal to 10 and also I'm
taking a decimal number double suppose D equal to 10.5 this is a decimal number I'm taking one
character c equal to a and also I'm taking one Boolean variable Boolean bull equal to true so
now I have taken four different primitive data types in the double carent bullet now I want to
convert them into string format so here there is only one method is available through which we
can convert any data type from any data type to string only one method for all kinds of data we
have only one single method which is available through which we can convert any data type from
any data type to string type that is value of okay value of method is available so scenario
two yes okay any data type to string so convert any data type of string this is the method string
dot value of here we can pass if you pass integer value in this it will convert into string if
you pass character Boolean whatever you want you can pass it here that will convert it into
string format so this method will convert all primitive data types into string format okay now
we'll see that how can we convert uh everything into string format okay first we will convert
uh integer so how can we do it simple we can say string dot value of this is the method you can
see there are how many formats are there Boolean character character array double flow in Long
everything this what type of feature it is one method many forms what type of uh concept it is in
objectoriented programming so as soon as you I'm searching for value of it is giving multiple
definitions so what type of feature it is it is overloading which is related to polymorphism
immediately you need to notice this okay whenever you see multiple methods as soon as you say dot
in Auto se that method is overloaded overloaded means what different parameters different data
types again rules are applicable so here we are converting uh integer to string type right so
we have to take this particular method value of and here we can pass integer variable so this
will convert integer into uh string format so now we can just print this string do value of
a like this okay or else if you don't want to print directly you can store that in a string
variable you can take one string variable here string s equal to string do value of a so now
this a is converting to string format so after that we can just print yes value like this so now
yes is a string variable so the value is there in the string format 10 is in string format okay so
similarly you can convert all other types to uh string format so for example let's say string
dot value of and uh d d is a basically double so that also I can store in a string yes see
already created variable here so I'm trying to reuse this variable here so whichever value we
have already stored in variable s the new value will be replaced here now again I can just print
yes value so this will print decimal number in string format right so same thing we can repeat
for other types so here this an integer decimal and character and guys tell me can we convert
character into string so this combination is possible or not so previously uh we are not
able to convert uh string into character right string to character not possible earlier string to
character not possible right because what string contains multiple characters car contains only
Single Character it's not possible but this time I'm trying to convert character into string
format is it possible or not character into string format yes possible why because string is
what it can have Single Character also multiple characters also right character is a single that
will converted into string so we will get a a one single character in string format string contains
a single character also right so for example uh if I look at this this is also string if I put
anything in the double quotation that is a string right this is possible so string to character is
also possible so when you run this it is converted character to string and we got the value similarly
Boolean is also possible so we can convert Boolean data into string format suppose here bull variable
I have taken so I'm just passing here bull so this is converted in string format again I'm printing
the same value after converting into to string so we got a string value okay so this is how we can
convert the data into one format to another format So within primitive types it is not possible okay
integer to flow flow to integer character integer integer character not possible but we can convert
string to any other types and any other types to string means all primitive types to string and
string to all primitive types is not possible it is possible so very very important scenarios
because in automation we will very you very frequently we use these data conversion methods
very frequently we use because whenever you work with the web applications when you read the data
from application on the UI everything will be in the string format so once you read the data in the
string format we can perform certain operations on the data so in that case we have to convert into
primitive types similarly whenever you want to test any application we have to pass the data into
the particular Fields like phone number first name last name email address and so on so whenever
you pass some data into the uh web application we have to pass everything into the string format
everything into the text format so in that case also we have to convert the data from primitive
types to string type of data so this is a most important so this is all about rapper class what
is a use of wrapper classes for every primitive type there is a corresponding wrapper class is
available and in the wrapper classes by using built-in methods we can convert the data from one
format to another format okay which is very very important and not only this the wer classes we
will use along with the collections also along with array list along with hash set hashmap along
with collections we use this wrapper classes we cannot use primitive data types in ARR list or
collection any other collections only rapper classes we will use in Array list for example
I'll tell you suppose what is an array list array list is just like an array okay so how can
create an array list you can say array list a RR equal to new err list so when you create an array
list object like this what will happen is in this we can store multiple values okay in the ARR is a
ARR list type of variable so in this we can store multiple values just like an array itself just
like an array it will give you uh we can store n number of we are not specifying any number of
locations because this is more Dynamic what is the difference between normal array and array
list is array is a static we have to specify the size but here we don't need to specify the
size so we can keep adding more number of values in this ER list as soon as you're adding more
number of values it will keep increasing this array okay suppose because if you create an
ARR list variable like this this will accept heterogeneous data means what here I can store 100
here I can store string here it can say character here it can say Boolean so heterogeneous data
is allowed by default but I want to restrict this with only homogeneous data I want to store
only integers I want to store only string I want to store only uh decimal numbers then how can we
do it you can do like this so you can say array list okay so you can write like this array list
and AR RR equal to new array list right this a normal definition it will allow all kinds of data
but if you want to restrict to specific type here we have to specify the type so here in the angular
bracket we have to specify wrapper class suppose if you want to store only integers I can specify
the wrapper class here here and also here here both the places in angular bracket we have to
specify so here we cannot use normal data type primitive data types not allowed so suppose if
you want to create this uh with a normal type let's say normal primitive type int here so this
is not valid this is not valid okay this is not valid this is valid so when you work with
the collections also we have to use only wrapper classes for representing the data we
cannot use primitive types in collections this is simple one example I have given so in the
next classes we will discuss in detail about collections concept so just I have given one
example so the wrapper classes will be used in two different ways one is for converting
the data into one format another format the second one is in collections in the collections
also we will use the the wrapper classes okay so remember uh this much for now and uh we will move
on to the next concept so is it clear everyone so far rapper classes data conversion methods
which is most important especially in automation many places we use this uh
data conversion methods from rapper class okay so now we'll discuss one small concept uh the packages and access modifiers
these are all miscellaneous Concepts okay we have already completed programming fundamentals and
objectoriented programming Concepts and these are all some more additional miscellaneous Concepts
which we are going to discuss so the packages and access modifiers so we are using these uh
almost every day but we don't know exactly what is package what is access modifier and uh how many
access modifiers are there which access modifier we have to use where we have to use so we'll
discuss in detail packages and access modifiers so what is package in Java so package is just
like a folder package is just like a folder if I go back to your uh project the project contains
the packages so normally what is a hierarchy okay so in Java normally what is a hierarchies first
we will create a project so project is a high level entity so this is the project this is the
project which we have created in Eclipse so the project contains the different packages these are
all packages so I say pack one pack two okay pack three and so on we can create n number of packages
and every every package contains a multiple classes class one class two class three and so
on every package contains a multiple classes so this is the hierarchy of java project project
contains a package package contains a classes so here package is just like a folder and all classes
are different files okay how it will store in the workspace just like a folders and files format it
will store okay if I look at here uh let me clear this okay so if I go back to the project and get
the location of your project in the workspace So currently everything is storing storing
into the workspace so let me just open my workspace okay so if I go back to the workspace
this is workspace now just observe how this folder structure is got saved so here SRC is a root
package now we go to SRC directly now we can see how many packages which we have created all
the packages are stored in the form of folders just like a folders okay and if you open any one
of the package let's say day 17 there is one Java file which we created if I open this you can see
the Java file which is got created so if you do any change here this will automatically reflect
here and again if you do change any change in this Eclipse that will automatically reflect here so
package is just like a folder and that will store like a folder in operating system okay so why we
need packages because we need to organize files properly so for organizing suppose in your system
you don't put all the files in one single place right you will separate the files audio files
are different folder personal files are different folder video files are different folder you can
keep multiple folders why we creating folders in your system because maintain so tomorrow if
you want to search for some particular file you can easily uh go and go to the particular folder
and you can easily get it so instead of keeping everything in one place and if you split them
into multiple folders it is easy to recognize the files and it is easy to maintain also so that
is the reason we have to use packages okay so in Java there are two kinds of a packages actually so
one is built-in package and user Define package so built-in package and the other one is uh user
defined package so user defined package means we created our own packages so these are all
packages which we created we have created all these packages okay these are all user defend
packages but there is another type of packages called buil-in packages buil-in packages is
nothing but the packages which are already there in Java already Java is already created
those packages and whenever they are buil-in packages definitely there are some buil buil-in
classes and whenever I say built-in class which which is also having buil-in methods so buil-in
package us a def package but what is an example of built-in package in the last classes uh we have
user somewhere suppose especially I think in Array concept okay so where exactly we use
uh the buil-in package so sometimes if I go back okay so in Array concept
I think when you print array we use something called util package okay Aras
do util or something I think it is seven somewhere yeah can just look at this here we are
accessing the predefined class array class and this is the predefined method so whenever you
are representing some built-in class we have to import that particular package so this array
class is present inside the java.util package okay java.util package so whenever you're trying
to use some built-in classes in your program you have to import corresponding package yeah scanner
is also uh built-in class scanner is buil-in class and where it is available scanner class is
available in which package again it is part of util package okay so like this suppose sometimes
if I use existing classes which are already there in Java we have to import corresponding package
then only we can access those classes otherwise we cannot access and this is applicable for user
def packages also if you have a two packages in one package okay suppose uh if you have uh let's
say if you create two different packages suppose you have a pack one and here I have a pack
two and here I have a class one and here I have a class two now I want to refer this class
here in another package so if you want to refer this C1 here how can we do it this class we
have to import in this C2 so pack one pack 1. C1 import pack 1. C1 we have to use this
import statement so that means whenever you want to use any external class or whenever you
want to use any buil-in class in your current program you have to import that package in which
that classes exist okay so that is a concept here okay guys understood what is the built-in
package and what is a user defin package so built-in packages are nothing but what those
packages which are already there in Java and if you want to use them you have to import it
and user defined packages means we can create our own package packages these are all our user
defin packages so in the coming sessions you will see so many built-in packages like
this n number of built-in packages once you start automation you can see n number of
built-in packages we can just import and use those classes and packages okay so package is
nothing but what it is just like a folder and we have two kinds of packages buil-in package
and user defend package and built-in packages whenever you want to use you have to import
that package then only you can access all the classes same thing is applicable for user
defend package and we can also create a folder within another folder right similarly we can
also create a package within another package that is called sub package we can also create
one package in another package which is called sub package for example let me show you here
currently we are in day 17 so this is a main package so how it is got stored in the memory
let me go back and open this folder so this is a folder so day 17 is got created as just like a
folder now which contains one file now inside the day 17 I want to create another package sub
package I want to create so how can we create sub package you can just right click here new
select package so on whichever package you want to create another package you can select that
package new package and here it will show you the current package so now we want to create a
sub package right you should not remove this you have to continue dot dot is representing sub
package dot then you can give any name let's say I'm giving pack one when I click on finish
so in Eclipse you can see the different entry here just like other packages this will also
show you another entry but if you go and see the folder day 17 inside the pack one is got
created in Eclipse the view is different in Eclipse just like other packages this sub package
is also shown as just like another entry it is not part of day 17 you can see here but if
you see the folder structure it is created a new pack one okay so this is how we can also
create sub packages so currently day 17 is having one file and one sub package very simple
concept okay now we will discuss about access modifiers so access modifiers so in
development point of view which is most important access modifier but automation
point of view is not that much important but interview prospective you have to know all
the access modifiers because everything we will make it as a public in automation we
don't maintain any private or security or anything for variables or methods so everything
we can make it as a public but in development point of view they have to make something
more secure and privacy so they will use some access kind of modifiers so there are
four types of access modifiers are available in Java listen this carefully very important
access modifiers four access modifiers there are four access modifier first one Public
public and second protected third default fourth fourth private so private is a very lowest
level of access modifier default is little higher than private protected is more higher than default
public is more higher than protector so basically what is access modifier access modifiers defines
the scope of a variable and methods so access modifiers defines defines scope of variables
and methods that means what where exactly we can access suppose if I create a variable where I can
access within the class within the package within the project different locations that is decided by
access modifier similarly when I create a method where I want to access that method within the
class or within the package or outside of the package that means within the project where I want
to access that is decided by the access modifier okay so access modifiers defines the scope of
variables and methods so in Java so we already seen the hierarchy how this hierarchy will be
maintained in Java s the high level entity is a project let's say this is a Java project this is a
Java project inside the project we have a packages there are n number of packages which we created
let's say this is P1 this is P2 this is P3 in every package we have multiple classes again there
are C1 C2 and so on C3 C4 and so on C5 C6 and so on so every package contains a classes now just
observe very carefully we will go from the bottom private so when I create a variable as a private
okay suppose in the class when I create a variable as a private or method as a private those methods
and variables we can access only within the class only within the class we cannot access anywhere
else totally restricted so wherever you created you can use only in that particular place you
cannot go beyond the class that is called private so the private variables and private methods
we can access only within the class which is restricted to the class that's it when I create
variables are methods as a default so default is not a keyword actually so we don't need to write
this keyword suppose when I create a method like this void M1 if you want to make it as a public
you have to write public keyword in front of this method if you want to make it as a protected you
have to write protected keyword if you want to make it as a private you have to write a private
keyword but if you want to make it as a default you no need to specify any modifier here just
leave like this this will be treated as a default access modifier so you don't need to especially
write a default keyword here not needed okay so if you don't specify any other three access modifiers
that will be treated as a default access modifier now if you make a method or variable as a private
we can access them only within the class now if you make the method as variables as a default
we can access them within the package within the package it is higher level of scope within the
package means what in all other classes which are present inside the same package we can access
means default is little higher than private little higher scope than private so private access
modifier variables and methods we can access only within the class default methods and default
variables we can access within the package within the package in the sense in all the classes we can
access within the package protected variables and methods we can access outside of the package
also but through inheritance is possible not not directly within the class is possible within
the package is directly possible but if you want access them outside of the package outside of
the package means what another package in some other class you can still use it you can access
it but through inheritance we have to use so this C2 Suppose there is a C1 in which you created
your protected variables and protected methods if you want to use them in the C3 you have to
make the C1 as a parent class of C3 three then only we will get them okay as soon as you make the
C1 as a parent for C3 whatever the variables and methods are belongs to parent obviously belongs
to child also right so through inheritance we can access the variables and methods outside of the
package that is protected public means everywhere we can access directly within the class within
the package outside of the package everywhere within the project wherever it is we can direct
L access without inheritance but what is the difference between these two protected also
we can access outside of the package public also we can access outside of the package but the
major difference between these two is protected variables and methods we have to inherit through
inheritance only we can access directly we cannot access but public we can directly access without
having any inheritance so these are the access levels class level package level and project level
three levels private means what only within the class default is nothing but only within the
package protected is nothing but what outside of the package only through inheritance public
is nothing but everywhere in the project every class every package everywhere we can access
these are four types of access modifiers now let me demonstrate this with an examples
quickly we will try to understand first very easy concept just only thing is you need to concentrate okay now okay let me take uh this day 17. pack one
itself and inside this I'm creating one new class okay so inside this I'm creating one simple
class called test one I'm just creating one new class called test one I'm not creating any main
method nothing I'm just saying only test one so in this just observe in this I'm creating one
variable inter X = to 100 and also I'm creating one method wi M1 and here I'm just trying to
print this is M1 from from test one package sorry M1 from test one or you can say simply this
is M1 now I have created one single variable one single method and I make them okay so let it
be there so also I will create another class I'll name it as a test two okay I'm just naming
as a test two class and here I will take main method so test one is a normal class which
contains one variable and Method and test two is a actual main class and both are there in
the same package or not both are there in the same package day 17. pack one that is a parent
package of both the classes currently both the classes are present in the same package remember
that now if you want to access the variable and Method normally what you will do you will create
an object of test one through that object you can access right so let's create object of test
one T1 t equal to new test one I created an object and through that object I can access let
me try to print t.x I'm able to access similarly I will call T do M1 perfectly right so when
I run this code it will access the variable and it will access the method and we are able to
get the output perfectly fine now what I will do is I'm accessing the variable and Method from
different class right test two is a different class test one is a different class here I'm
able to access now what I will do is I will make the variable as a private also I will make
the method as a private both I will make them as a private after saving this observe here here it
is giving an error the field test one dox is not visible same error M1 also the method M1 from
the type test one is not visible why it is not visible because we make them as a private what is
the behavior of private access modifier private access modifier the variables method we can access
only within the class only within the test one I can access but we are trying to access outside
of the class right that is not possible so that is a private access modifier so private access
modifier means you can access only within the class completely restricted access only within
the class so when you make private variable and Method we able to access only within the class but
when you're trying to access outside of the class it is giving an error so that is a first access
modif private variables and private methods we can access only within the class now let us make
them as a default command this and I will create the same variable same method this time I don't
specify any access modifier if I don't specify any access modifier what is an access modifier will
be allocated for this default access modifier so default is little higher than private so private
variables and Method we can access only within the class whereas default variables and methods we
can access within the package within the package means what test one test two are belongs to same
package or not yes so the variable and Method we can directly access here as soon as we make
them as as a default we are able to access in outside of the class also so when you run this
code this will return the output for us we are able to access here why we able to access because
they are the default variables and default methods we can access in multiple package we can access
in multiple classes which are belongs to the same package okay private is a class level default is
a package level that means only within the package we can access within the package we can access
okay let us try to access outside of the package and then we will see whether he able to access or
not so what I will do is I will create this test to class in another package okay let me create so
in the day 17 itself I'm going to create dot pack two another package observe very carefully okay
now what I will do is I will get the I will copy this test 2. Java into pack two okay now observe
very careful just see the test one is present in pack one test two is present in pack two now
currently the method and variables are default earlier we were able to access because test one
test two are there in the same package but now test two is separated in pack two another package
so here it is giving an error so first error what it is giving it is giving this error in the class
itself why because the test two don't know about the test one where the test one is got created
because we completely enter into the different package and the test two is part of pack two
we are referring test one so test two class doesn't know about the test one from where it is
coming from so now what we need to do we have to import the package whenever you are referring
the classes from external package you have to import that package first how to import you have
to write import statement import which package under day 17. pack one dot test one you have to
import the class from this particular package and then you can use it here you can create an object
okay this issue is got solved now what about the variable and Method when you put the cursor here
it is saying the field test 1.x is not visible and also M1 is not visible why they are not visible
because they are default variables they are default methods you can access only within the
package but here we are trying to access from different package that is the reason we are not
able to access so default access modifier we can access only only within the package within the
package means again all the classes which which are present in the same package so earlier test
two is a part of pack one we are able to access here this test two is a pack one here we are
able to access but another test two which I have created as part of pack two in this we are
not able to access okay this is second type of access modifier default now protected protected
is little higher than default so let me command this so now observe carefully so protected is
we can access within the class we can access within the package directly and we can also
access outside of the package also outside of the package means in the test in the test two
from pack two also we can access let's make them as a protected see them protected and also I'll
make this method as a protected so now I have a protected variable and protected method but still
here we are not able to access even though we make them as a protected outside of the package we are
not able to access so when you make variable and Method as a protected we cannot directly access
only through inheritance we can access that means direct access means what like this t.x t one
but directly by object we will able to access but when you make them as a protected we cannot
directly access but through inheritance we can access outside of the package means everywhere in
the project you can access when you make it as a super class how can we make it test two extends
test one this is a concept so now what happens test one from pack one is becomes a parent class
of test two now as soon as the test one becomes a parent whatever is belongs to test one which is
also belongs to test two also right test two also so now when you create an object of test two test2
T2 equal to new test2 through this object we can access everything right so I can just print T2
dox I'm able to access similarly T2 Dot M1 I'm able to access so the below one is a direct access
actually but when you're making as a protected we cannot directly access we we we have to make
this class as a parent class and extend into child class while creating an object of child
class we can access them this is indirectly we are accessing so still we have to know we have to
do import why because we are still referring the test one class from the different package right so
whenever you are referring the external class we have to do import this UT is for what because
we are using external class which is external class means what from different package from test
one from tack one okay input is must but we are accessing the variables and methods through the
child class object not directly directly means what we can directly create a test one object
and through the object if you if you are able to access that is the direct access but here
we are not directly accessing we are indirectly accessing through child class object that is
protected so private means within the class default means within the package protected means
what outside of the package also we can access but only through inheritance it is possible if
you have more than one class you can import you have to write multiple inputs okay multiple inut
suppose if you have a multiple classes from the same package suppose we are referring multiple
classes from the same package you can simply say star you don't need to write multiple inputs star
representing all the classes which are present in the pack one and if you want to import a specific
class you can mention the name like this okay if you have a more than one class you can also
still import more than one class means you can simply say star and if you have one class to UT
you can simply say one class name okay now just observe here private is for Access only within the
class default we can access within the class and outside of the class means within the package we
can access protected means outside of the package also we can access but we through inheritance it
is possible in public we can directly access no inheritance No Object nothing so let me show you
Public public let's comment this public access modifier so instead of protected I can make it
as a public public variable and this is a public method okay so now I make them as a both are
public now come to the package here we don't need to extend now so once we make them as a
public directly we can access like this we no need to extend so what I will do is I will just uh
make another entry this is extension class command this okay now I'm just making another copy here okay just a moment so this commented from here okay so I have commented this
entire class now come to the CH class so through inheritance we don't need to do it to remove this part even extend also
not required okay extension is also not required so now you can see public class test two
and main method and inside this we can directly create an object for test one and from the test
one we can directly access because they are the public we can access everywhere directly we can
access just by creating an object and access so now we got the output so here we able to access
the variables and Method from pack one to the pack two just by creating an object of test one
so this is the direct access everywhere in the project in whichever package you want to access
you will able to access okay so these are all four types of access modifiers so the access levels
are different so private means only within the class private then default means what package
only within the package default and protected means what outside of the package if you have
any other package here also we can access that is protected but only through inheritance it is
possible and directly if you want access we have to make it as a public everywhere in the project
you can access if you want to make it as a public import is always necessary whenever you want to
refer external class from external package it is not related to accessing the variable or accessing
the methods okay before that suppose this is one package package one and this is another package
okay this is the package two here you have a class one and here class two now this class I
want to use it here for this C1 class I want to create an object here okay so C2 is present in
the pack two but here you are referring C1 which is actually coming from different package right so
here you have to import import package one do C1 after importing you will able to access C1 class
that is a necessity of import statement whenever you are referring the external class external
class means from external package you have to use import then only you will able to access
the class once you are able to access the class you can create an object through the object you
can access variables and methods okay so this is the concept of access modifier so protected access
modifier also we can access outside of the package but only through inheritance it is possible Public
Access modifier we can access everywhere public access to modifier we can access everywhere so
these are the four access modifier most of them we use only public okay clear everyone about
access modifiers public protected default and private okay so practice uh these Concepts
rapper classes packages and how to work with the multiple packages how to use
access modifier so just do one round of practice that is enough uh in the next class
uh we will discuss about uh some other topic like exception handling and then we have
collections all those things okay so we will see in the next class I'll stop here for
today's session if you have anything we can discuss
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