right so today we'll see uh one of the
most important topic so which is uh typ casting so let us try to understand what is
typ casting and how many ways we can do type casting and how can we do typ cast primitive
data types and how we can type cast objects so there different concepts are there so
we'll see in the today's session step by step so very simple way of understanding type
Casting means what we can convert the type from uh we can convert the data from one type to
another type okay we can convert the data from one type to another type which is basically
called as a type casting okay and again in the type casting we have a two kinds of a type
casting we can call it as a upcasting and then downcasting or we can say converting type of data which is called type
casting and there are two kinds of type castings one is upcasting
which is also called as widening process okay widening and uh second is downcasting just a second downcasting this
is also called narrowing just a second so upcasting which is also called
widening downcasting which is also called narrowing so these are the two types of uh
type casting techniques are there in Java so upcasting and downcasting and this upcasting
and downcasting both castings are applicable for primitive types and also object types okay
we can also use it for primitive data types and also objects that means it is applicable for
object oriented programming also so now we'll see with an example first we'll start with the
primitive data types we will try to understand what is upcasting and downcasting then we will
apply the same concept for objects okay so first of all let me just go to Eclipse directly and
uh create new package day 18 okay now in this package I'm going to create new class I'll
name it as typ casting concept typ casting concept I'm taking main method all right
so first of all let us see type casting of primitive data types so first of all what
is a typ casting so as I said first one is uh upcasting means what converting the
data or we can convert the uh okay we can convert the data from smaller
type to higher type smaller to higher converting data or we can say converting
value just a second converting value okay for converting value from smaller to to higher smaller to or larger okay so we
have a different type of data types right we have a bite short int long double float
there are different data types are there so we going we are going to convert the value
from smaller type to higher or smaller to larger which is called upcasting same opposite of
downcasting is what converting the value from larger to smaller larger to smaller
larger to smaller which is called down Casting downcasting upcasting means what
converting the value from smaller to larger type downcast in means converting the value from
larger to smaller type so let's see some example if I look at that upcasting example let's say
I have some data which is an integer format which is an integer format and I can convert this
into long format okay or long format well look at here which one is higher size integer or long
long in the sense long in in or long which one is higher long is having higher size so here
what exactly we are doing we are converting from smaller type to higher type or smaller
to higher okay or smaller to larger so this is called upcasting upcasting means what lower
to higher or smaller to larger which is called upcasting same thing downcasting means what for
example let's say or we can take another example let's say I have double type decimal number
that I want to okay I'll say float just a [Music] second so let's say I have a float float is another type right
so float is another data type now if I convert float to double which
one is lower which one is higher float is a smaller type float is a smaller type double
is a higher type we can store uh more value in the double right so smaller to higher so flow
to double if I convert from flow to double which is also upcasting so basically Casting means
what converting the value from one type to another type again in the upcasting means lower
to higher in is a lower data type long is little bit higher than in same thing float and double
float is a lower type double is a higher type so smaller to larger which is called upcasting same
opposite of this is downcasting so for example if you have a long type or long int from that we
can convert to the int int is a lower type okay so larger to lower you downcasting it actually
downcasting means higher to lower and same thing for example say double double is a higher that
we can convert into lower which is float double to float this is called downcasting upcasting and
downcasting upcasting means what converting the type from smaller to larger type downcasting means
converting the value from larger to smaller type so this is a theoretical definition so so now
we'll see uh just a second guys can you see my screen okay can you see my screen okay all right so this is a basic understanding
is upcasting means what converting the value from smaller to larger downcasting means converting
the value from larger to smaller type okay now we'll see an example so let us say uh upcasting
is an automatic process upcasting is a automatic process means what we can directly store integer
value into the wrong and we can directly store float value into the double so there is it is
an automatic process we don't need to do any extra thing but when you do down casting we
have to do casting so we need to do this do casting uh explicitly that means manually we
have to do it and upcasting is an automatic process downcasting is a manual process that
means we have to explicitly do the downcasting so let me show you an example first we'll see
upcasting upcasting is nothing but what it is an automatic process we don't need to do
any extra thing do to do upcasting okay means what smaller to higher or smaller to larger
upcasting means smaller to larger now I'm taking one variable called in uh int value equal to
some 100 so this is my integer variable okay and which one is a larger type then integer we
have a long or long in you can take anything so now I'm taking one variable called long
and long value equal to in value can you do like this is it correct so what exactly I'm
doing is I'm assigning the integer variable into long variable okay so integer value is a
smaller value long is an higher value it can hold higher value right because long is having
more space than integer so smaller value I'm assigning into higher value that means small
small to higher smaller to or larger which is called upcasting okay so now if you just want to
print the value of long value you can just print it this is upcasting so this is automatic
process we don't need to do anything when you do upcasting so we can assign the lower
value to the higher variable example we can assign the integer value to the long because long
is having more SP is than integer okay this is upcasting and are we doing anything here extra
nothing we are just assigning the value into the another variable we are assigning the variable
into the another variable that's it we are not doing anything extra so this is automatic process
so upcasting is a automatic we can just assign the lower type value to the higher or smaller
to the larger which is called upcasting so let me show you another example so let's take uh
a decimal number float float value equal to 10.5 okay can we deare declare the float variable
like this float float is nothing but what it can hold a decimal number can we Define the variable
like this float float value equal 10 float value is nothing but just a variable name so we need to
assign something called literal okay whenever your working with the float we need to use a literal
so let's say F upper case or smaller case f you need to put at the end of the value now can I
store this float into double let me take one more variable called double double value equal
to float value can we do like this yes because double is a higher type than float double is a
higher than or larger than float so the float value I can assign into double this is a direct
approach we don't we are not going to do anything else okay we directly going to store a smaller
value into the larger value this is also called as upcasting this is also upcasting example for
upcasting so we are basically storing a smaller value into larger that's called upcasting
concept okay let me show you uh downcasting example so upcasting means
what we can store smaller value into the larger now downcasting is exactly opposite downcasting this is not automatic we
need to do manually and uh downcasting means what we can convert larger type to smaller type
larger type to smaller type let's see some example so this time I'm taking long variable long long
value equal to I'm taking a big number like this okay now I'm taking one integer variable int value
equal to Long value can we do like this int value equal to Long value so the problem here is in the
downcasting the major problem is in the upcasting what exactly we are doing we are assigning the
lower value into the higher variable okay for example let's say so this is a variable or this
is a space I have let's say this is a space and this is higher space can we store this into
higher space smaller thing we can store into higher right so smaller we can store into higher
so this is what upcasting which we have understood so smaller value we can store into higher variable
so upcasting this is possible so this is automatic process because there is no problem in this
because small item we can put into the larger basket right this is no problem here upcasting
is automatic but when you want to put larger item into smaller thing this is a downcasting so here
the problem comes so we want to store the larger thing in the lower thing or the smallest container
so what is a problem in this so first of all can we put like this the larger value into the lower
variable We cannot put first of all we cannot put but how we can do it we have to reduce this size
okay one way is what we have to reduce this size we can make this as a small size and then we can
put it so to reducing this part into smaller size we can call it as a casting here and also when
you're trying to reduce this big one into smaller size there is a chance of missing or trating some
values okay because we are trying to reduce this into smaller size before storing right so whenever
you're trying to reduce this into smaller side there is a chance of losing some values or losing
some or Tran some data that possibility is always there in the downcasting okay I hope now you
understood upcasting means what smaller to larger downcasting means what larger to smaller so larger
to smaller is always a problem so that is the reason we have to reduce this into smaller size
and then we will store into variable so what we can do now is we have to reduce it how to reduce
so long value I'm going to store into integer variable that is the reason here we have to put
something called int so now what we happen is the long value whatever we have taken here that will
be converted into integer format and then it will store into integer variable okay so this is called
downcasting we are storing the higher value into the lower variable but normally it is not possible
so what we are doing here we are converting or we reducing the longer value into the lower lower
value and then we are storing into the lower variable this is called downcasting we are trying
to reduce the level so upcasting and then down casting okay and this is manually we have added
this this is explicitly we have to do it it is not automatically done okay so this is a concept of
upcasting I'll tell you where we have to use just a moment let me first make you try to understand
so downcasting so let me show you another example next suppose I'm taking a uh decimal number I'm taking a double double double value
this is a decimal number let's say 1 125 do 55 so this is a decimal number double and I'm
taking one float variable float value equal to double value see this I'm trying to assign
larger value into the lower variable larger to lower we are converting it is not possible
so what we need to do now first we need to reduce the size of the value for that we need
to specify casting like this so we will convert that into float and then I'm storing into the
float okay what exactly we're doing so double means it is having higher value that we are
reducing and storing into the float so higher to or larger to smaller which is also called
as a downcasting this is also an example for the downcasting and again what is a problem
in the downcasting suppose in this double value it can hold larger value right suppose
if you're storing a big value like this but this much of value float cannot hold in that
case what happens some value to be missing or some value will be terminated after that the
value will be stored into the float variable so there is a chance of missing or trating
the verginal value so there is a problem in the downcasting okay if I take smaller values no
problem but if I take larger values you may face this kind of an error so this is a also called
as a downcasting now I will write something here you guys can tell me which is upcasting
or downcasting okay so I'm taking one more example you mention the data type after the
variable name will there be an impact so this particular data type after the variable name okay
we'll see see okay so let me put this here okay we can't specify like this okay the type cting means
that should be before the variable that should be before the variable like this okay now we'll see
the one more example you guys can tell me whether it is an upcasting or downcasting and what is
the problem we will have in the downcasting we will see this I'm taking one integer variable
in I equal to 100 integer variable I'm taking in I equal to 100 and I'm taking Double D equal
to I first of all is it possible or not integer and double can we store integer value into
the double yes because both are having both are numeric types only right both are numeric
types but integer cannot hold decimal number but double can hold integer right so double is higher
than integer always so what we are doing we are storing smaller value into higher variable smaller
into higher now is it upcasting or downcasting it is obviously upcasting so this is an automatic
process we are not going to do anything so now let us print the value of D okay what is the output
you getting here value of D we are converting integer to decimal so as soon as we convert US
to decimal by default it will add decimal number so here you will get 100 do0 okay now I'll
will show you another example this is called upcasting upcasting so now let's see another
example example two right so this time I'm taking double see now you'll understand the
problem with the downcasting okay double D equal to 10.5 I'm taking okay and then I'm taking
one integer variable in I equal to D can we do like this directly no because double is a higher
integer is a lower we are trying to store larger value into the smaller variable so it is not
possible so how we can we do this we need to reduce the size so we need to put tag cast int
so we can convert this double into integer and then we can store into I then what is a problem
in this when you print I value the decimal part will be terminated okay so we will not get the
actual value which is 10.5 so this is a problem which we have in the downcasting so this is also
called as downcasting operation downcasting okay so guys now we understood what is upcasting what
is downcasting upcasting and downcasting so we'll apply the same part we'll apply the same thing
for objects and object oriented programming also so once you understand then I'll tell you when
exactly we use upcasting when exactly we use down casting okay so this is the concept very
simple upcasting and downcasting so upcasting is nothing but converting the value from smaller
to larger type downcasting means converting the value from the larger to smaller so for to
do upcasting you don't need to do any extra thing you can just assign the value directly
so this is automatic process but downcasting is what in explicitly we have to specify by the
data type before because we are trying to reduce the size of the value okay this is upcasting
and downcasting now let us try to apply the same concept for objectoriented programming
or the objects how it is going to work for the objects now most important little bit confusing
topic but you need to carefully listen primitive types it is very simple but when you come to
object type you need to follow certain rules when you do the type casting okay let's say uh
type casting for object I'm taking this another example typ St type casting objects one so if you trying to do type casting for the
objects how it is going to work let me take uh let me create few classes here because I need
to create an object if you want to create an object there should be some class so we can
also apply the same type casting concept for predefined objects predefined classes also
so I'm going to create some user different packages let's see I'm creating one class
called parent class and in this I'm taking one variable called string name equal to let's
say John and also I'm creating one method void M1 and in this I'm going to print something
called and this is y1 from parent okay this is implementation of parent class now I'm going
to create another class called child class and in this also I'm taking one more variable called
int ID I'm directly assigning the value also then void uh M2 I'm creating one more method
called M2 in this I'm going to print this is M2 from Child class okay this is implementation
so now this child is extends from parent so now is there any relationship between child and parent
is there any relation between parent and child yes so the parent is child there is a relation now
okay so this child is extended from the parent law so whatever the child we created which is
having everything from the parent okay now let us try to do the upcasting and downcasting of the
con type casting for the objects okay now let us see the example come to the main class okay in
the main class what I will do is I'm directly going to create an object for the child class
observe very carefully I'm just going to show you multiple combinations so normally what
you will do if you have a parent class and child class normally which class we will create
an object if you want to access everything child class we will create okay let's create a child
class object child c equal to new child okay so when you're creating an object for the child
class we'll able to access everything from child and we'll able to access everything from child as
well as parent so now what I can do is by using the C object I can access name actually this
is coming from which class parent class okay which is coming from the parent class perfectly
fine we're able to access now can we access C dot uh M1 M1 is also coming from the parent
yes we can still access and similarly can we access c. ID yes because ID is belongs to child
class itself so this is coming from Child class we can access C do M2 M2 is also coming from
the child class we can also access no problem in this so when you create an object directly
object is child class and reference variable is also belongs to child class both are same okay
object is also child class reference variable is also child class both are same there is no
difference so we're able to access everything from the parent and and child so in this there
is no problem at all okay now I'm going to show you something here let's com this this is one
approach now what I'm going to do is here I will say I'm going to create reference avilable
of parent P equal to new child can we do like this is this correct syntax parent P equal to new
child obviously why because parent class reference variable can hold child class object okay remember
always parent class reference variable can always hold child class object okay what type of
casting it is upcasting or down casting child is the smaller parent is a higher because
child is derived from the parent right so what kind of casting it is downcasting or upcasting
we are storing the lowest object into the higher variable parent is higher child is a lower or
parent is a larger child is a smaller so we are trying to store smaller object into higher
variable right so this is possible so this is called what upcasting we are doing upcasting okay
so when you do this what is the what we can access what we cannot access so let us see this what
we can access access what we cannot access if you do like this parent P equal to new child if
you create like this object is what child class object variable is what parent okay now you
tell me can we access same thing I'm going to write okay so same thing system. print let
me comment this first system pinell p. name can we access or not p. name yes see the problem
here is when you do upcasting we are storing the child object into the parent so what we can do
is whatever the variables and methods are there in the parent right only those variables and
variables we can variables and methods we can access even though the object is created for the
the child we cannot access everything from the child we can access everything from the parent
only why because the variable is a parent okay so if you're trying to access name from the parent
yes we can access this is perfectly fine and when you're trying to call M1 method p. M1 yes still
you're able to call because name and M1 is belongs to parent but observe when you're trying to access
when you're trying to access ID and and M2 method we cannot access here it is giving an error why
because even though the object is got created for the child class but the reference variable is
what reference variable is a parent right so that is the reason we are able to access variable
and Method only from the parent but not from the child class so here we cannot access this is
a problem in the upcasting we cannot access so we cannot access okay so when you're trying to access
the variables and methods from the parent class we can access why because the reference variable
is a parent but from the object we cannot access anything ID and M2 these two are belongs to what
child class so we cannot access them if you have created reference variable for the parent
we can access only the stuff from from the parent we cannot access anything from the child
class okay so this is one example okay now let's do the downcasting so now this time what I'll do
is child c equal to new parent now tell me is it correct or not child c equal to new parent we are
trying to store the larger object into the lower object downcasting and when you do downcasting
we have to do explicitly we have to specify what we need to specify in the down casting what we
need to specify the variable we have to specify okay so what we can do is first let's create one
parent variable parent P equal to new parent okay and then what we can do now this P I'm going to
assign into C so parent P equal to new parent so I have created an object for what parent now I'm
trying to assign this parent into child so higher to lower higher to lower so when you do higher to
lower what we have to do we need to do type casing so which class we have to specify here which
class we have to specify child got my point so we are trying to store higher into lower which
is called downcast so when you do downcasting explicitly we have to specify the child class
we are reducing the size parent to child okay so now by using the C object what else we can
access now can we access C do name c do name yes we can access system. printel can we access c. ID
yes we can access then C do M1 we can access C do M2 also we can access so this is a solution so in
the downcasting we can access everything now what is an error is ging Class cast exception see the
problem we are not getting any error compile time error but at the run time we are getting an error
here this is a problem with the downg gting at the run time we are getting an error so why we are
getting an error here means this is a parent class object and we are trying to store that parent
class object into child class also we have done the typ casting so why still it is not working so
compile time there is no error here but only at the run time we are getting an error why we are
getting run time because p is what parent class variable and what is an object which is referring
actually parent class object right parent class object it is referring so the parent class object
and child child is a lower than P child is a lower than P right so at the run time this one and this
one is having a mismatch so that is the reason it is giving a typ casting exception type casting
exception okay so parent P equal to new parent in another way I can store so for example let's say
we have seen this combination parent P equal to new new child parent P equal to new child child c
equal to new child so in this there is no problem but in this there is a problem when you do the
uh downcasting there is a problem okay so this is a this is called downcasting This is called
downcasting because we are trying to reduce the parent into the child Okay so this is the problem
we cannot we can access name and ID M1 M2 but at the run time we will get an exception when you're
working with a downcasting to the objects okay so when we have to use this approach and when we have
to use this approach so let me tell you something here child c equal to new child so this is one
type of notation we are using and P P equal to new child this is another type of notation we are
using so both are correct right so both are this is equivalent class no problem here in this there
is no downcasting there is no upcasting because the same object we have storing into the same type
of a variable so there is no downcasting there is no upcasting here but in the second option what
we are doing here we are doing upcasting so lower value we are assigning to the upper so this is
the one okay okay now when we have to use this approach when you have to use this approach okay
so to understand this I will give you uh one more example with that we are trying to understand let
us say uh I have a small array okay let's say I taking one array here and this array contains a
different type of data so let us say it can you can consider this an array or a list whatever
so here I have an integer value here I have a decimal value and here some character here some
string value here I have some Boolean value okay this is my array and in this array I don't know
what type of data we have okay what type of data this array contains I don't know at the runtime
but what I'm doing at the run time is I'm trying to extract something let's say a of let's say
this is 0 1 2 3 4 4 a of two I'm saying when I say A of two what is the value you are getting
here a of two means what a it will return the character okay suppose if you are not aware of
this data like what type of data you are having in this array right if you want to store this
value in some other variable so you don't know which what type of data you are getting from a of
two a of two what type of data you getting this is an array right from a of to what type of data
it can be return integer it will return decimal or it will return return character or string you
don't know that okay you don't know what is exact value what is a specific value are rning in that
case you will specify generic type generic type is nothing but a object object because you don't
know exactly what type of data you are getting here right object is what object is a root class
of all the classes or root type of all the types right it can hold everything it is a parent or
super parent of everything in Java so if you don't know exactly at the run time if you don't
know exactly what type of data you are returning or what type of data you are getting then you
specify the object okay this is a super parent of all the classes okay for example a of three
so in a of three we have a string so this time I know exactly this will return return a string if I
know it is returning a string then what I will do I will Define X is a string directly right so if
I don't know what is a type it will return then I will use object if I know what is a value it will
return then I will specify the exact type if it is written a string then I will specify string if it
is written integer I will specify integer if it is a written Boolean then I will specify Boolean but
if I don't know what type of data it is returning then you can specify the object or any parent
which can hold this one okay now you understand this scenario everyone right when we have to
specify this object when you have to specify in the specific data type if you know exactly what
type of data it is rning you can specify that if you don't know what type of data it is rning you
don't know then you can specify this same thing is applicable here so suppose if this object is rning
what exactly this object is returning if you know that you can directly specify here child it is
rning so I'm just specifying the child variable here the reference variable is also ched okay
suppose if this object returning something but we don't know exactly what it is rning right then
instead of specifying the exact same type you will specify the parent because the parent can hold
child right parent can hold child so if I take parent reference able this can hold parent type of
object and also child type of object parent type of object and also child type of object so if you
know exactly what type of object you are returning you can specify that as a you can specify with the
same type if I don't know exactly what is rning you specify that as a generic type another example
I will take suppose I'm taking here s equal to welcome s equal to welcome okay now we are storing
this value into the S so you know exactly this is a string then you can specify yes is a string this
is the direct this is a direct if you know what is the value which we have so that you can directly
specify the string this is more specific suppose if you don't know exactly suppose again welcome is
there but again you don't know exactly what type it is okay for example I'm expecting this value
from the user okay s value uh by using scanner class I'm expecting this value into the S variable
but what type you will specify for this S I don't know what type of value the user can enter user
can enter integer user can enter float or user can enter string you don't know what type of value
I will get at the runtime so then what type you will specify for this variable object because
this is a parent of all the parents parent of all the classes in Java so it can hold everything
you understood now so if you know specific data if you know exactly what kind of value are getting
specifically then you will specify that time if you're getting string you will specify the string
if you're getting integer then you'll specify the integer if you're getting the double then you'll
specify the double it is more specific but if you don't know what type of value you are getting
at the run time you don't know then you will specify a class which is a parent of all the
classes string int float Boolean everything we can store into the object okay so now you
understood when we have to use this when you have to use this if this object is rning the if
you know what exactly this object is rning then you can specify the type if you don't know what
exactly the object is wring then you can specify the parent or object anything okay so this is all
about upcasting and downcasting concept everybody is clear so far with this example so we will see
more examples about typ casting because when you do typ casting objects we need to follow certain
rules and conditions so we will discuss all those rules and conditions now so I'll try to elaborate
this concept and give you more examples so that you will be more familiar so this is another
important INRI question okay when to use this approach when to use this approach so CH class
object we can always store in the parent class variable this you have to remember child class
object we cannot we can also always store in the parent variable okay so grandparent no if you have
grandparent can we use them no grandparent uh here yes here you can use grandparent no problem parent
of parent is called grandparent so that is again see I'll tell you an example one more example
now you guys can tell me it is correct or not okay so I have a class B Class and C Class okay
uh I will write multiple combinations you guys can tell me with this correct syntax or not a
a equal to yeah valid or not valid or not yes this is valid now tell me a a equal to B valid or
not valid or not yes or no valid by because child class object B is a child of a right child class
object can hold in the parent class variable yeah this is also perfectly fine now can we do like
this let's say a a equal to C is it valid or not it is also valid why because C is a child
class of b b is again child class of a right so a is higher than C or not higher so object
of C can we hold in a or not yes obviously we can hold now B Bal to C is it valid or not b b
equal to C valid or not yes C is a child class of B right the object of C we can store in
the reference variable of B so the parent class object can hold CH class perfectly fine
okay perfectly fine now b b equal to b equal to actually we should say new keyword in every object
I have not specified new can we do like this or not new a into B I'm storing the larger object
into smaller object downcasting so what we have to do in this case here we have to specify B type
casting type casting is needed then only we can do it otherwise no okay now just observe here b
equal to new C valid or not is it valid or not valid why because C is a child B is a parent so
we can directly hold child class object into the parent class object so I hope everybody is clear
but when you do downcasting what we can do is we cannot access certain variables from the child
class so that's the reason downcasting is not preferred most of the times you understood the
point everyone so now now I'm going to show you some more examples and we need to understand
the rules first when you do typ casting or upcasting or down casting whatever we have to
remember certain rules otherwise you will get a compile time exception and runtime exception
okay so now let me show you some more examples of casting so it is just a terminology upcasting
and down casting right so let's go to the another example so we will try to understand the rules
so this is not possible let me remove this first okay either go with this one or go with the
this one now let me try to show you one more example typ casting objects to so when you do typ casting of objects we have
to follow certain rules okay there are some rules are there so we can use downcasting we
can still use downcasting for the objects but there are some rules we have to follow okay
so I will show you what are those rules we have to follow and accordingly we can do the
downcasting so now let me show you something like this so forget about downcasting upcasting
those terminologies are not much important just understand the concept upcasting means what
lower to higher downcasting means higher to lower it can be primitive type or it can be object
anything is is applicable okay now we have to know uh certain rules when you do or when you are
working with the casting concept okay so what are the rules and regulations we have to follow
so but before that uh I'm going to create a few classes here class animal okay uh I'm not adding
any variables and methods I'm just want to show you the type casting okay so animal is one class
can we create a class like this one single line because I don't have any variables and methods
so instead of keeping these brackets in multiple lines like this right I can put them in the one
single line one single line is enough like this if I don't have any variables or methods just
if you want to just create an empty class you can create like this okay now I'm going to create
another class uh dog which is extends from animal and this is also Meed class now I'll create
another class called as a cat which is also extends from which is also extends from same class
called animal this is also empty class okay this is also empty class and so now one parent
is having two child classes animal dog and cat so animal is a parent class which is having
two child classes dog and cat and is there any relation between dog and cat these two classes
any parent child or any relationship between these two no there's no relation between dog
and God both are two independent classes dog is derived from animal cat is also derived from
the same animal class remember this these two are the child classes of animal animal is a parent of
dog and cat but there is no relation between dog and cat remember this hierarchy okay so now let
us try to do the typ casting whenever you do the typ casting we have to follow three rules okay
three fundamental rules we have to understand if you understand these three rules you can easily
do the type gting for the objects okay so let us see what are the rules and uh let's go to main
method directly first so here what I'll do is I will create animal a n equal to new animal I am
creating uh the object is also referring to the animal class and the variable is also referring
to the animal class so both are same animal a n equal to new animal instead of that I'm taking
dog can we do like this dog CH CL object we we can store in the parent class variable perfectly
fine child class object I can store in the parent class variable okay perfectly fine now I will
try to do the casting like this just observe I say cat CT equal to a n and here I will say cat
so this is the kind of a casting I'm doing and if you just observe this part in this a is what
animal type of variable or animal type of object cat and we are converting this a into cat and then
we are storing into the cat type of object okay so whenever you do type casting in this expression
we will have okay in this expression sorry guys yeah in this expression we will have three parts
actually if I look at this there are three parts are there let me write it here and we need to
understand the rules first okay and then we will try to understand see in this expression
there are this is the first part second third and fourth divide this entire statement into four
parts okay and let us call them with some names okay let's say this is a this is B this is C this
is D and classes I'm representing with uppercase characters objects or reference variable I'm
representing with the lower case character so let us divide this entire expression into four
categories let's say a b c and d a is a Class C is also Class B is a variable D is also a variable
okay let's divide this entire thing in the four parts now based on that let us try to understand
the rules what is the first rule rule one is the rule one is whatever variable we created here
what is the type of this variable animal and whatever the class we specified here this should
be either equal to animal class or it should be or the first rule is what there should be some
relation between these two actually the rule one is there should be some relation between c and
d d is what a variable C is what casting type so if you look at this expression the first rule
is what whatever variable you are using this one and this one so between these two there should be
some relationship there should be equal or there should be some parent and child relationship if
I look at this a is a animal class of variable cat is what child class of animal so both are
having some relationship or not these two are having some relationship or not yes or no yes
there is a relationship so the first rule is let me write here rule one when you do type
casting we have to follow this rules rule one is conversion is valid or not we have to check
conversion is valid or not conversion means what we are converting this into cat typ so this is
called conversion here we are doing conversion is it valid or not we have to check so how to
check this conversion is valid or not so the type of D and C must have have some relationship
so type of d and c means what c and d means what these two these two c and d means these two
cat and animal these two parts so these two are having some relationship or not if there is
a relationship the conversion is valid if there is no relationship the conversion is not valid now
is there relationship between these two or not yes a is Der from animal only right animal cat is a
child class of animal so relation is there if the relation is there then rule one is satisfied or
not rule one is satisfied or not yes so according to the rule one the first verification is what
the conversion is valid or not the conversion is valid or not how to check the conversion is valid
or not so the type of this a and the type of this cat both are having certain relationship it can
be parent and child or it can be child or parent whatever there should be some relationship okay
then only the conversion is possible this is the first rule so in this example this is valid as
per rule one okay not only this rule there are another two rules we have to check then only the
type casting is possible so this is a rule one everybody's understood about rule one so in this
typ casting conversion is valid or not we need to verify how to ver the conversion is valid or
not these two what are these two actually we are referring this as a d this we are referring as
a d that's the reason I'm giving some names to this part A B C D okay in this expression C and
D should have some relationship if C and D is having some relationship then this conversion is
valid if there's no relation between C and D this conversion is not valid so then rule number one
will not be satisfied so as per this expression as of now the cat and a which is derived from
animal class both are having relationship so this expression is valid the conversion is valid
as per rule one okay now next one suppose let me write like this you guys can tell me valid
or not I'm rning dog DG equal to new dog okay now I have created an object for the dog class
also the reference variable is also dog class okay now I'll say cat CT equal to DG and here
the conversion type is cat now tell me is it valid according to rule one according to the
rule one is it valid or not according to the rule one C and D that means these two are having
some relationship so what is a DG here DG is a dog type of object what is a cat a cat is another
class so dog and cat do we have any relationship there is no relationship so the conversion is not
valid conversion is not valid so according to the rule one this expression is valid or not not valid
okay this is invalid according to or as per rule one okay now you understood so rule rule one rule
one this is Rule one again we have two more rules the first rule is what C and D both should have
some relationship then only the conversion is valid this is a rule one okay now let us discuss
the rule two when you do the type casting rule two rule two let me write a point here so what is a rule
two let me write something here animal a n equal to new dog I'm reading object like this and then
I say cat CT equal to animal and the conversion is called cat just observe very carefully as per
rule one is it valid or not as per rule one rule one yes why because as per the rule one C and D
both should have some relationship so here cat and animal both are having relationship parent and
child relationship because cat is different from animal only right so rule one is valid rule one
is valid now let us check the rule two is valid or not in the rule two what we have to check
is assignment is valid or not in the rule one conversion is valid or not we will check means
C and D having having relationship or not now in the rule two assignment is valid or not we need
to check in the rule two assignment is valid or not we need to check what is an assignment this
is called assignment we are asscending this into CT that is valid or not we need to check but how
to check C must be either same or child of a here I'm talking about C and A C and A should be same
or C must be child of a c must be child of a what does it mean if you look at this after conversion
where exactly I'm storing it after conversion this become cat right cat object that we are storing
into another variable this variable also should be cat type why because we are storing the cat
object into cat into variable so that variable should be what cat type of variable you understood
this point the assignment is valid or not the assignment is a valid or not so let me show you
an example for example I'm I'm taking one integer variable some x equal to okay I'm taking one
character variable some x equal to okay a now I'm assigning the X sorry and I'm taking Y and then
I'm assigning X is this assignment valid or not is this assignment valid or not assignment not valid
why because X is a character Y is an integer so X cannot store in the integer variable character
value we cannot store in the integer variable so this is invalid assignment right same thing when
you come to the object here after conversion it becomes a cat object this returns a cat object
and if you want to store that cat object in a variable that should be which type cat type
only right then only it can hold so that is called assignment is valid or not so the C must be
either same or child of a what is a here this is called a b c d right this is a so the C is nothing
but what this is C so the C must be same as a or the C must be the child of a you got my point
so this must be exactly same as this variable or this must be the child of this class because
the child class object can hold in the parent class variable everybody's clear about this point
or is there any confusion so in this rule two we are checking assignment is valid or not assignment
is nothing but after converting this variable what type we will get cat type so if you want to store
cat type in the variable the variable should also be cype or the parent of cat it can be parent also
it can be animal also no problem okay so that is a rule number two assignment is valid or not C
must be either same as a A or parent of child of a so that is a rule number two okay so this is
valid in this case this is valid or not according to to the rule two is it valid or not is it valid
or not according to the rule two as per rule two rule one anyway it is valid because a n is an
animal C cat is a child class animal both are having relationship so rule two anyway satisfied
rule one is anyway satisfied but I'm talking about rule two so whatever the type we are storing into
the variable so this should be same type so when I storing the cat type of variable in this variable
then it should be also for cat type okay so this is a valid as per rule two now I'll show you one
more then you can tell me whether is valid as per rule two or not so I'm creating one variable
called animal a n equal to new dog okay now cat CT equal to dog now tell me is it valid or not first
check the rule one as per the rule one it is valid or not as per the rule one is it valid or not so
rule one is what these two are having relationship so animal is deriv from animal dog is a child
class of animal so both are having relationship so rule one is satisfied rule one is satisfied
now what about the rule two we converting this into dog but storing into what cat type of
variable cat type of variable so what is the rule to conversion is Rule to assignment is valid
or not how we can check the c c is nothing but this one must be either same as this one or the
dog should be child of cat right so there is no relation between these two so rule number two is
failed so this is invalid as per rule number two rule number two it is invalid statement
understood everyone why it is invalid according to the rule number two because we
are storing this dog object into something else which is cat cat and dog there is no
relation there's a reason as per the rule two it is invalid so what is the rule two
assignment is valid or not we need to check rule one conversion is valid or not we will
check okay so these are the two rules so these are the rule number one and rule number two now
rule number three so rule number two understood everybody so don't go about variables and methods
okay if you include them you will be confused just focus only on the type casting part and what
are the rules we have to follow when you do type casting and once you typ casting what and
all we can access what and all we can access that is a different thing thing okay don't go
that area so just stick to this okay when you do typ casting what are the rules we have to
follow so first rule I've shown you example second rule I have shown you example now we'll
go to the third rule third rule okay so when you rule one or rule two is not satisfied then
you will get an exception see suppose uh rule one is not satisfied so you will get a compile
time error here itself it will show you cannot cast from dog to cost okay cannot cast from dog
to class if your rule one is failed or rule two is not failed in both the cases it will give you
compil time error so you can just look at this even this also invalid right so if your rule
two is also failed it will give you compiled error but if rule three is failed execution
will happen you will not get any compile time error but you will get runtime error so what
is the rule three we will try to understand so don't talk about variables and methods
for now okay just stick to only object type casting what are the rules we have to follow
that's what I'm saying right so now rule number three what is the rule number three so rule
three is the underlying object of type D must be either same or CH class of C so I'll tell
you what exactly it is the underlying object of type D must be either same or child of C so
let us try to understand this with an example again I'm creating animal class object equal
to new dog okay I'm storing the child class object into parent class variable fine
now I'll do type casting like this dog DG equal to Casting dog and then animal
so now tell me is the statement valid or not or I can take this one sorry I can take this
one cat CT equal to cat animal now tell me is the statement is valid or not this par statement is
valid or not so are you seeing any compile time error here are you seeing any error in this
no error but when you run this you will get a runtime error okay when you run this you'll
get a runtime error so what is an error Class cast exception Class cast exception so if rule
one and Rule two is got failed you will get a compile time error but when you rule three is
got failed you will get a runtime error you will not see any compile time error you will not see
any syntax error here but you will get a runtime error so now we'll understand what is the rule
number three so the underlaying object of type D what is D here in this expression a an this is a
d so the underlying object of type D is what the object of a an is referring to which dog class
object so a an is referring for what dog class type of object object so the underlying object of
type D is what dog must be either same or child of C what is C here this is called C this is D this
is a c so what is the point here the underlying object of a an is what dog and here it is what
cat do you have any relationship between these two dog and cat no relation so what is a point
here is the underlying object of a n should be same as this cat or the child of this cat Okay
this should be same as a cat or this should be the child of a cat then only this rule will be
satisfied now as per this particular expression is it valid or not according to the rule three
according to the rule three is it valid or not a underlying object of a an is what dog but what
is the reference we specified cat is there any relation between the dog and cat no that is the
reason this is invalid as per rule number three it is invalid as per rule number three okay the
underlying object of d means this one what is an underlying object of this animal dog and c means
cat should have some Rel relationship or should be same then only this statement is valid so the
underlying object is different here the casting object is different so it is invalid as per rule
three if it is invalid you are not getting compil time error but you will get the runtime error so
if you rule one and Rule two is not satisfied you will get a compile time error when rule three
is not satisfied you will get a runtime error that is a rule number three okay now you guys can
tell me whether the type casting is valid or not according to all the rules rule one rule two and
Rule three okay now I'm combining all the rules rule one rule two and Rule according to all three
rules this statement is valid or not you have to tell animal I'm creating animal a equal to new
dog and now I'm doing the typ casting called Dog DG equal to dog of animal okay first what is the
first rule what is the first rule these two are exactly having relationship or not so this is
a this is B this is C and this is D so between these two is there relationship or not dog and
animal yes or no yes so animal is derived from animal a is derived from animal dog is a CH class
of animal so relationship is there so rule one is satisfied rule one is satisfied okay rule one
is yes satisfied now come to the rule two so after conversion we are storing into this variable
right and what type of this variable it should be dog type so what type of object we are storing dog
type of object what type of variable it should be dog it should be same type or it should be parent
type here we can also specify the parent of dog so matching or not yes rule two is satisfied or not
yes so rule two is satisfied now come to the rule three what is the rule three the underlying object
of this a what is an underlying object of this a dog is an underlying object and the conversion
object what is this dog so both are same or not yes both should be same or it should be parent of
this one so rule three is also satisfied so rule three is also satisfied so if both three rules are
satisfied then only this typ casting expression is correct otherwise even one rule is not satisfied
this type casting expression is not valid okay now everybody's understood about three rules so
when you do type casting all three rules should be applicable even one rule is not satisfied that
expression which we have written is not correct rule one is what the relationship between C and
D what is a rule two first rule one as uh rule one these two are having relationship or not rule
two assignment is correct or not after conversion we are storing into the right variable or not the
convers assignment is correct or not rule three is what the underlying of object of a n that is
new Doc should be same as this one or not if these three rules are satisfied then typ casting
statement is correct otherwise no okay so if you divide this typ casting expression like a b c d
then it will be clear rule one is what conversion is valid or not how to check conversion is valid
or not because both are having some relationship and assignment is valid or not that means whatever
object we converted we are storing into the right variable or not that is called assignment is
valid or not rule three the underlying object of a n should be equal or the parent of the cat
glass that is a rule number three if three rules are satisfied then the type casting is correct
so now everybody's understood about all three rules which are very very important so when
you do type casting for objects you have to remember these three rules okay if these three
rules are satisfied then object type casting is correct okay now in this example we have
seen the typ casting for objects which are user defined we have created our own classes and
objects right and how the same type casting is applicable for pred def classes or predefined
objects which are already exist in Java so we'll see another example anybody's having any
queries on this example before showing another one don't uh think about variables and methods access levels and everything it here
just focus only on the type casting part we will see all the combination okay I already told you when we have to use uh
these two approaches in this example I already told you when you have to store this in the
child class when you have to use a parent class two different situations two different
scenarios if you exactly know what type of object it is wring you can store you can directly
specify the exact name and if you don't know what type of object it is wring then you can just
specify the parent but what is a problem in these two approaches in the first approach we
can access everything from child and parent but in the second approach we can access everything
from parent but not child that is a limitation okay if you have variables and methods that is
a process okay now let me show you uh another example yeah you can run it no problem even if you run
this now you won't get any runtime exception or nothing because this is Val I have not
printed anything right so I'm not getting anything the next example I will show you
the values also okay some put some value variables and methods and we'll see that
so there is no runtime exception there is no compile time exception so if the
rule one and Rule three are failed you will get a compile time exception and if
your rule three is got failed your program will execute but you will get a runtime
exception if rule three is got failed all right so now let me show you one more example
for predefined classes so type casting objects three okay so type casting objects three okay so now this example I will ask
some questions you guys can tell me and don't forget the rule okay so when you write
a uh type casting expression this is the rule this is a Formula I can consider a b equal
to C and this is a d this is the expression writing a type casting expression syntax of
writing type casting expression okay now so remember all three rules rule one rule three
and Rule three rule two and Rule three so we need to remember those rules before doing
it okay right so I'm going to show you some examples you guys can tell me whether they are
valid or not according to the rules so before telling you need to check the rules okay here
I'm creating I'm uh here I'm using a predefined classes predefined objects so I'm not going to
create any new classes so I'm taking an object type of variable object W equal to new string
of Welcome is it valid statement can we store string object into object variable yes because
object is a parent of everything right so we can store string type of object into parent class
variable this is perfectly valid statement okay now here I'm taking string buffer variable
string buffer is also one more predefined class which is available in Java okay so string
buffer SB equal to and I'm doing some kind of a casting here string buffer of wo now tell me
according to the rule one is it valid or not according to the rule one is the statement
valid or not so what is the rule one rule one conversion is valid or not and these two
should have relationship or not right A B C D so C and D is having relationship or not yes or
no W is object type of variable string buffer is another class and this is also derived from the
object class because object is a parent class of all other classes so is the relationship or not is
there any relationship yes so rule one is passed rule one is satisfied rule one is satisfied okay
now I will write something like this rule one is satisfied and Rule two rule two is satisfied
or not rule two what is the rule two after converting this we are storing into some variable
and that variable should be same as this one or the parent of this one satisfied or not yes it is
same exactly so rule two is also satisfied rule two is also satisfied now what about rule three
rule three what is the rule three the underlying object of wo is what string the underlying object
of w is what a string and what is the reference we are using string buffer string and string
buffer is there any relationship no there is no relationship between string and string buffer
so rule three is got failed in this expression rule three is got failed you understood my point
rule one rule three rule three so all three rules should be satisfied then only this expression is
correct otherwise wrong okay this is one example so you have to tell me okay now the second example
let's take another example example two I'm taking string variable string s equal to new string and
here I'm keeping some value okay string is equal to new string now string buffer variable I'm
taking string buffer SB equal to string buffer SB equal to Casting is what string buffer and here
I'll say yes now tell me is it valid statement or not rule number one what's the rule number one C
and D these two should have a relationship do we have relationship between this one string buffer
and S string buffer is a different class yes is a string here so this is a different class there is
no relationship so rule one itself is got failed rule one is failed if rule one itself is failed
we no need to check the other rules no need to go with rule two and Rule three there is no use
right so rule one itself is got failed so in this expression rule one itself is got failed so you no
need to check rule two and Rule three now another example here I'm taking object o equal to new
string welcome right statement is valid because string is a child class of object so the string
object I can store in the object type of variable so this is perfectly correct now I'm using
something called string buffer string buffer SB equal to Casting is string buffer and say o now
tell me valid or not first rule number one so rule number one satisfied or not rule number one rule
number is what rule number is one is what these two should have some relationship so string buffer
and object is having relationship or not yes because string buffer is again derived from object
itself so both are having relationship that means what rule one is satisfied rule one is satisfied
now rule number two rule number two is what after conversion we are storing into the variable and
the variable should have same type or the parent typer so currently sting buffer and sting buffer
both are the same types both are the same types or not something wrong in this string buffer is there any spelling mistake yes here
is a spelling mistake string buffer okay right so rule one is satisfied
because both are having relationship rule number two is is what after conversion we
storing into the exact variable or not yes so rule two is also satisfied rule two is also
satisfied now rule number three the underlying object of O is what string the underlying object
of w is what string and what is a reference we are using here is string buffer so there is no
relation right string and string buffer there is no relation so rule number number three
passed or failed failed rule number three is failed so this expression is invalid even
one rule is not satisfied that expression the type casting expression is invalid okay
rule three is not satisfied but rule one rule two is satisfied so this is one example
now let me tell you one more example example four example for here I'm taking object
variable object W equal to new string of new string of welcome new string
of welcome and I'm taking string buffer SB equal to string is a type casting
string and here I'll say o now rule number one rule number one and three are not same
okay rule number one we are checking the relation between these two in rule number three
we are checking the underlying object the type of this object is what string and this one
okay we are checking the underlying object this one and this one should be same that is
the rule number three rule number one is what these two are having relationship or not direct
so o is what object type string buffer is what child of object so rule number one is satisfied
but rule number three string buffer is wo wo is what underlying object of is what string is
underlying object so string and string buffer is not matching that's the reason rule number
three is got fed okay rule number one three is totally different now see this expression
in this rule number one is satisfied or not rule number one yes or no rule number one there
should be some relationship in this so o is what object type string is also CH class of object
so relationship is there or not between these two yes so that means what rule number one is
pass rule one is passed now come to the rule two rule two is what after after conversion
string should be stored in a variable and that variable should be again either string or
the parent of string which can be object so is there any relation between string and string
buffer after converting into string we are storing into SB SB is against string buffer so
this is not valid assignment so rule three rule two is also failed rule two is failed actually
rule two is a failed so if the rule two is got failed we want need to check the rule three again
if rule one is passed then only go to rule two if the rule two is passed then only go to rule
three okay so here rule two is got failed so you no need to check the rule three so this is
one more example okay so now let me take another one so you need to be more familiar with the three rules that's the reason I'm telling
all these examples so next one I'm taking string variable called s equal
to new string and here I'll put some value and now I'm taking string buffer SB
okay equal to Casting is a string and yes now tell me rule number one is valid or not
rule one passed or not so the VAR here there should be relation between these two that's
the first rule so s is what string type of variable again here also string both are
equal right so relationship is there it should be same or should have a parent
and child relationship anything is fine so rule one is what pass now rule two so after
converting into the string we are storing into another variable that is again different class
type string buffer so string and string buffer is totally different so here also rule two is
failed rule two is got failed it is invalid as per okay now another example example six example six here I'm creating an object
type of variable object W equal to new string of Welcome object W equal to new string of welcome
so here W is O is a object is O new string so O is a parent class variable which can hold string okay
now I'm writing like this string buffer string buffer SB equal to Casting is also string buffer
okay and here I say o now tell me rule number one pass or failed pass why because o is an object
type of variable string buffer is a CH class of object so both are having relationship right
relationship so rule number is passed now rule number two rule number two after converting we
are assigning into SB so we are converting string buffer we converted into string buffer and storing
into the same typee of a string buffer here also string buffer it should be same or it can be
parent so rule two is also pass rule two is also passed now what about rule three so underlying
object of w is what string the underlying of object of O is what string and here it is a string
buffer so string and string buffer not matching no relation so rule three is got failed rule three
is got failed okay now another example example seven again I'm taking object variable equal
to new string welcome object o equal to new string of w come I'm just trying to make you
understand the concept that's the reason I'm giving more example and certain things are similar
here but I'm I'm just trying to make try make you understand about rule one rule two and Rule
three if you are familiar with this you will able to do the type casting okay don't compare
this okay these combination if you are already using somewhere earlier don't compare like this
just check the rule one rule two and Rule three Prospect so example seven object equal to new
string of welcome and here I'm taking a string variable string s equal to string of O yes tell
me rule number one passed or failed rule number one rule one rule one is what again there should
be some relation between these two string and W is an object so there is a relation so rule
one is passed what about rule two rule after conversion we are assigning into a s so after
conversion becomes string and the variable is also string so rule two is also passed rule
two is also passed now what about rule three rule three the underlying object of w is what
string and reference here is what string both are same or both should have some relation so
rule three is also passed rule three is also past so this is the exact right statement to
do the right type casting okay now if you're trying to print this can we print this s
variable can we print system print and if you print s what you what is the value will
get same welcome you will get because after successful conversion because rule one rule two
rule three all three rules are satisfied so the typ casting statement is perfectly correct so
you will get a output like this okay okay so now everybody is clear about all three rules
how to check all the three rules when you do type casting all three rules how to check all
three rules for the type casting so these are for predefined classes and predef objects okay
so this is all about type casting concept it's all about typ casting concept so we have seen what
is typ casting upcasting and downcasting also we have seen typ casting of primitive variables
also we have seen typ casting of predefined classes also we have seen typ casting of uh user
defined classes all three different combinations okay in the first example I already told you typ
casting for primitive types in the second example I told you the parent and child relationship and
if you create an object directly like this we can directly access everything from parent and child
but if you create like this if you are able to hold child object into the parent then you're
able to access only stuff from the parent not from the child not from the child so that is a
limitation which we have but when to use this one and when to use this one if you know exactly
what type of object it is rning at run time then you can specify the exact type but if you don't
know what type of object it is wring at the right time then you have to specify the generic
type that can hold everything that is normally object okay understood everyone so this is
all about type casting concept so this much is enough guys okay no need to go in depth of
this but in selenium once you start automation you will see some scenarios uh where you
will see this kind of a type casting thing okay there again I will show you where exactly
we use this type casting in automation okay so now if you have any questions you guys can
ask so let us stop here for today's session and tomorrow we'll discuss uh another
topic we have two more topics exception handling and then we have a collections
so we will discuss those Concepts in the next session then we will finish it off
all right so that's all we stop recording
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