Explore object casting in java | parent p = new child() | upcasting with dynamic method dispatch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back so in this it is a video we are going to talk about up casting and down casting in Java and also we are going to understand why we need up casting and what is the benefit of using of casting and what is what is down casting and why we need down casting okay so we're going to talk about it step by step but in this particular tutorial we are going to talk about off casting in Java and we are going to write a lot of code which is best enough casting okay so let's get started then [Music] all right so now let's create few classes here okay so now I need to create a class here let me say class and let me give a class name here mobile okay so class Mobile I'll open it here and close it here let's say in my mobile class I have a simple method called calling so I'll write void and calling and open it and close it and I'll give the parenthesis sorry color process here and let's say that I have a simple print statement here inside this calling method let's say calling from let's say calling only okay so there we go okay so I have a class here called mobile I have a function called calling and inside the calling method I'm just printing a statement calling all right and now let's say I have another class here and my class name is Samsung okay so my class name is Samsung here and this Samsung is a mobile right this Samsung is a mobile isn't it okay so this Samsung and this mobile has a easy relationship right and this is a relationship is called as inheritance and to use inheritance I have to use this keyword called extends right so class Samsung extends mobile so Mobile is the superclass for Samsung right again I'll open this class here and let me close it over here okay and in my Samsung class let me override this calling function here so me override this let's say void calling again and let me give it a buddy here and let me say a system dot out dot println and let me say calling from samsung mobile calling from samsung ok so there you go and now let's say this samsung has a new picture in it if you use this samsung mobile you can also use camera right let's say this plus samsung is providing an extra feature let's say camera ok and I'll write system dot out dot println and let's say camera on this is give a dummy implementation right nothing else ok so there we go I'm done with it okay so I have a class here called Samsung and the Samsung has a parent class which is mobile this mobile has one particular function called calling and this particular Samsung class is overriding this particular function called calling and it does have an extra feature services camera ok so now let me represent this entire stuff here diagrammatically right so that we can see it better right we can experience it better ok so let me draw some diagrams here so let me say this is my mobile ok this is my mobile class this think like this is the class or objects or things like that so this is my mobile and inside this mobile I have a function here calling so let me write over here calling and I have an end of the class called Samsung ok Sam Shoen and in this Samsung class this calling feature I'm operating and I do have another method called camera so let me let me say it has to function let us say calling and camera right so there you go so let's think like this is my inheritance tree super class subclass mobile and the subclass is Samsung right ok so now let us understand of casting ok so we are all set so what is our casting so casting a subtype to a super type is called a casting casting to super type is called of casting so what I mean by that okay so let me first create a soft type object okay so let me create a subclass object over here so my subclass is Samsung so let me create the Samsung object so I'm going to use a different marker here so let's say Samsung and I'll say Samsung is equal to new Samsung very simple very straightforward this is how we create a object right so if you talk about this object right now this is is referencing to Samsung class right so this is is subtype reference isn't it because this is belongs to Samsung this one and Samsung is a soft class right so now I want to cast it to the super type let's say I have I am just writing this particular reference here so as I said casting something to super type is called up casting okay so now this is subtype and if I am casting this to the super type what is the super type Samsung super type is mobile so let me write mobile here there you go so simple this is called up casting this is what soft type okay this is subtype subtype reference s is subtype reference now I am casting this subtype to super type so what is the super type mobile right so I am writing mobile over here so there you go so right now this particular thing doesn't have a reference right so let's give it a reference so what what I have written over here this is the mobile type right so I have to write mobile here this is mobile so I am writing here mobile and let me give it a reference m and you are done so this is what we call off casting okay [Music] [Music] [Music] so if you talk about this thing this is what you are doing you are even actually doing up casting here so if you talk about this particular thing okay I can I can write it in a simple simple way so to achieve this thing you have written two lines two lines of code over here right you have written this two lines of code here so let me convert it to a one-liner okay and it's very simple so here what we are doing we are creating a surplus object and once the object is getting created here we are getting the reference and this reference were converting to or we're storing it in a super type so first we are creating the object right so let me create the object so how to create the object of this subtype because I'm creating the subtype object here so a new Samsung this is what I am doing here right Newsham shown and this is the subtype object and I am storing it in a super type reference so who is my super type mobile right so I can write mobile here and all right I'll just give it a reference that's the M so this is how we are doing up casting in a one line right mobile M equal to new Samsung Mobile is super type and Samsung is sub type there you go simple right all right guys so now let's have a simple explanation for this so we have our super class we have our sub class mobile is our superclass and Samsung is our sub class so op casting is casting a sub type to a super type right casting to super type is called up casting so let's create a sub type object over here so Samsung is our sub class so I'm creating a sub class object so now let's cast it to super type so s is sub type right so s is sub type reference now I'm casting it to super type Mobile is my super type so I am casting the sub type two super type now let's give it a reference so this is mobile so I have to store this inside a mobile reference so Mobile is super type s is soft light so s is sub type and I have casted it to mobile which is super type fight so casting a sub type to a super type is called up casting and this is the prototype so I'm storing it inside a super type reference right so basically what I'm doing over here I'm storing this subtype into a super type reference right this aim is holding s right now and this s is a reference which is pointing to the Samsung class upset okay so indirectly we're storing the Samsung class object inside the parent class reference isn't it so as we go further in this tutorial we're going to understand off casting is safe of casting is natural of casting is implicit we don't need to cast it explicitly like this okay it's fine if we're doing it but as a developer we shouldn't do it because this is implicit okay it can be happen automatically so as a developer we shouldn't write it to say about time so let's remove it so right now I can move this edge to here right so now this aim which is super type is directly holding the sub type reference because this is natural I don't need to do it explicitly all right so now this s is sub type this M is super type my super type reference is holding the subclass object so this is a reference here now s is pointing to Samsung class object right so what I'm doing technically over here my superclass reference is holding a sub class object right so in Java we can store a sub class object inside a parent class reference okay so now let's make it simple so here what we're doing this s is pointing to so s is a reference right and this s reference is pointing to the Samsung class object so let's create the Samsung class object over here so new Samsung it will create the Samsung class upset and now let's store it in a parent class reference so let's do it over here mobile M so mobile M equal to new Samsung so that's what we are doing it over here so mobile M I have written it over here mobile M so M is my parent class reference and inside the parent class reference I am storing the Samsung class object so that's what we are doing over here inside the parental as reference we're storing the subclass reference so this reference over here is pointing to this particular object right so so indirectly we're storing the subclass object here inside the parent class reference so this is equal to this so here is the conclusion as you can see over here and over here as well we are not casting it explicitly so up casting can be done implicitly up casting can be happened automatically and JVM is going to take care of that okay so all casting is natural up casting yourself and now let's dig into that because we don't need to do up casting explicitly all right but before we go further I just want to tell you that in this tutorial I will do some example by doing the casting manually I mean by doing the up casting manually just to make you feel comfortable without casting just to make you feel that what casting actually means so that you will not be confused and you'll get this concept clearly without any confusion all right so now let's go ahead so either you can go for this or you can go for this but the result is same we're doing up casting here we're casting it to mobile and we are doing the same thing we're creating the object and once we are creating the object we are storing it in the reference of a parent type ok mobile is parent type over here so the subclass object we're storing it in the super type reference right so we are storing this Samsung here in the mobile we're storing this Samsung in the mobile right so we're going like this isn't it we are going like this so we're going up so let's let's think like this is your inheritance treat okay so we're going up right so we're storing this subject in is reference so we're going up right so this is what we call op casting simple right so either you can write like this or you can write like this okay so in the both the way what you are doing is you are casting a subclass object into a super type simple casting something your super type casting something is super type is called op casting because you are going upwards in the inheritance tree right so that's what of casting [Music] [Music] okay so now let me talk about one of the confusion that a lot of people have okay so this thing like I have written two lines of code over here okay so I have a mobile reference and I have a Samsung reference so how many object has been created if you talk about this two lines of code is it one or is it - and if I'm going to print system dot out dot println of em what it is going to give it to me this Samsung object or mobile object so now this is a confusion a lot of people have right so our let's solve this out so if you talk about here whenever I write this code okay Samsung is equal to new Samsung obviously a object is getting created in the memory and this is Samsung objects Samsung class subject okay ah Sam Shoen object so I'm creating the Samsung object so this s is referencing to this particular object right so whenever I'm trying to cast it to mobile what is happening so this s object okay I am trying to store it with the parent class reference so this is a reference right this aim is a reference I'm under storing this sub type in a parent class reference and so this is just a reference this aim is right now pointing to this object only which is the child a class object okay which is the sub type object here okay it is not creating a different object this is one object only this s is pointing to this object and this M is also pointing to this object so think about it if I if I'll do system dot out dot println of s what it is going to give it to me okay system dot out dot println of s so this s this S is pointing to what the Samsung object right so it is this give something something like Samsung at the rate plus name at the rate some hexadecimal value let's say 1/2 a be some hexadecimal code ok so makes our decimal number okay so again if I do system dot out dot println of s sorry M then what it is going to give it to me okay so now to see okay in the M what we are storing the same object here right we're storing this s here in M right and this M is also pointing to the same object here right with this child object over here right so if when you are going to print this M here it is also going to give you the same reference okay so this particular thing you are also going to get if you are going to print the M reference here okay because both m and s is pointing to one object which is samsung class subject okay so you are going to get the Samsung class reference here if you're going to print it okay and similarly if you are going to print it here system two doubted println of M so what I'm going to get here what object you are storing Samsung of that you are creating a samsung object and Samsung class object and you are storing this object with a parent reference right so this object is this particular class object right Samsung class object so you are going to get Samsung at the rate sorry Samsung at the rate some hexadecimal value right simple right so this is one of the confusion a lot of people have because even I had this confusion when I started learning Java right so this is very simple right [Music] [Applause] [Music] [Applause] [Music] okay so now let me do one thing let me call all these methods we have okay using this reference okay and let's see how this is working and what output that we are going to get okay so let me wipe these things out okay so let me do of testing here so let me say let me do it in a one line so let me say what is my parent type mobile I write mobile and m equal to I store the child class object here new child class is Samsung so new Samsung right so this is what I am doing up casting here and I'm storing the Samsung object in the mobile so I'm storing this in this I'm going going off here so this is up casting so right now is it as you already know if I will print out this M what it is going to give it to me it is going to give it to me this Samsung class reference so I'm going to get Samsung at the rate some hexadecimal value and then if I am doing m dot calling so if you talk about this mobile this is M is mobile dev reference this M is parent type reference right so if I am doing calling here m dot calling this particular method is going to be get called right so see over here we have the calling method in the super type and we have a calling method in the subtype so whenever I am doing MDOT calling compiler is going to check first okay is this calling method is over here is this calling method is present inside the mobile class so the calling method is present inside the mobile class right this particular method is there inside the mobile class so this compile is fine but whenever JVM is going to run this I mean when JVM is going to execute this particular program it is going to check so what daivam is going to check this M is a mobile type and this M is storing the object of Samsung class so in the Samsung class it is going to check whether app calling method whether the calling method is operate then here in the Samsung class and in this case we have operated this calling method here so this particular method is going to be execute because we have operated it okay so calling from Samsung is going to be the output we are going to get this particular function executed and we are going to get this particular line in our output right calling from Samsung so this is what we call dynamic dispatch right calling from Samsung okay okay so now let's answer me if this particular method is not here okay it suppose less that this particular method is not here and I'm using and if I am doing MDOT calling what particular method is going to be get called think like this is not here so I'm doing MDOT calling so in the parent class this calling method is there yes it is there so if this is there is it operate then what or this particular M is touring Samson Samson to a subject so in the Samson class is it operate dead no it's not operating if it is not operating this particular method will be get called the parent type method will be get called over here okay so right now if I'm going to do M that camera okay if I'm doing MDOT camera here now this thing what is going to happen okay this is the confusion okay a lot of people get confused about it okay MDOT camera so right now image of what type mobile type right first of all you are going to get a compiler son error now you might be thinking why I'm going to get a compile as an error because you are doing MDOT camera so this M is the parent type reference and in the parent class in the mobile class do we have a method called camera no we do not have a method called camera here so you are going to get compliation if you compile as an error over here okay so because this particular method is not available right so you might be thinking okay so I'm storing the Samsung class object here I'm storing the Samsung class object here in the EM reference so why not I can access to this particular camera okay here this particular camera method so you have to understand Java it's very Sept so Java is going to answer in this question okay what if you are not going to stir Samsung object I am safe I cannot take risk right if you want to have it then you just do down casting okay so we're going to talk about that later right first of all you have to understand here if you are using the parent class reference okay and if you're storing the child class objective in you cannot access the child class method so this aim is belonged to the mobile type reference and in the mobile type reference we do not have a camera method so we cannot call this camera May 30 and if you are going to call it you are going to get compliation error because Java is theft device Java is going to tell you what if you are going to store something else here okay how can I be sure sure okay so because of the safety you know if you are trying to call this particular camera method then the camera method has to be here in the parent class okay so be sure about it okay so this is what this is how we can call the methods that we have created over here okay so how to call this camera method using the parent class we are going to talk about it in the next chapter I mean in the next tutorial where we are going to talk about down casting in Java okay so I took now we are talking pretty much about the up casting so now let me do one thing let me do all this thing that I have created over here in the computer okay so let's go to the computer and let's do all these things practically all right alright guys so whatever we have done in the tutorials so far right now we are going to do the practical of it and also we are going to learn few new things here and right after this practical session it would be just for next five to seven minutes and after that we'll go through a exercise okay so I'm going to go for a different scenario and you have to guess the output for all the scenarios okay which will make us comfortable enough casting okay and in that point of view you have to carefully understand whatever I'm saying over here okay so few things you have already understood and few things we're going to explore right now okay so let's get started so first of all I'm going to create a class here called mobile and in this mobile class I'm going to have a method called calling and in this calling method let's say I have a simple system dot out dot println and let's say calling here and right now let me create another class here called Samsung and the Samsung is a mobile so it's extends mobile and right now in the Samsung class this Samsung class is overriding this calling method so let me copy and paste it over here and right now the Samsung class provides a different implementation for this calling method let's say calling from Samsung mobile all right so right now let's use this to particular class over here just to experiment I'm going to create another class called use mobile and there we go and here we have a man matter so let me quickly type that in and let me write here string arguments so first of all we're going to do a casting here so let me create the object of Samsung class and let me store it with mobile manually so let's create the object of Samsung class Samsung as a kill 2 News Champion and right now let's cast the subject and here I'm going to cast it to a super type which is mobile and if this is mobile I need to store it with mobile and then I'll give it a reference less them up here and there you go so right now we got our reference here so I can call the methods which is available in inside the mobile class inside the mobile class we have the calling method so we can call it easily moved earth calling and first of all let me compile this particular program and let's say we are getting the expected output or not let me zoom this thing and let me do a clear and there you go so first of all let me do a Java C my class name is use mobile dot Java and is compiled fine so there is no error over here right so this is the thing to notice and let me do run it huge mobile and there we go calling from Samsung mobile so we're getting the output as expected calling from Samsung Mobile because this calling method has been operating over here okay so this the thing is very simple and just to make sure we cannot call the camera matter okay so I just forget to add the camera meter here so let me add the camera meter here go ahead camera inside the Samsung class and let me do a system dot out dot println and camera on okay so this camera method is in the Samsung class so I cannot call the camera method here just because I'm using the parent class reference right so if you can see here this mobile is the parent class reference right and inside the parent class we do not have the camera method we do have only one method so if I'm going to run this right now let me run it again Java see mobile door Java and I'm getting the comparison error as you can see it's just complaining for this okay all right guys so one more thing that I want to tell you over here that whenever you do a casting like this this is same as this so you are creating the Samsung class objects or new Samsung and as we have discussed we can directly store it in a parent class reference my parent class is mobile here it's a mobile em right so this is also going to work fine so if I'm going to compile here so let me do a clear screen and if I'm going to compile here Java C use mobile dirt Java I can say this compiles fine this does is not Kappa it's not complaining about it okay so this another thing to remember here okay so now let's learn some exciting things okay so okay so before we go for this okay so now let's learn some new thing and so let's say if I am giving you a number let's say 10 okay so how you're deciding you know in which type in which in which data type you are going to store it so by looking at the number you are deciding right so you are going to store it in an INT or integer right so I can write integer I equal to 10 right so this perfectly valid okay so how you've decided by looking at this time right and just tell me one thing you can also store this number in an object right because object is the superclass for every class in Java and this stain is of integer type so I can also store this 10 with a object type isn't it because this object is the superclass of integer class and similarly whenever you are doing casting here so right now let me remove this so if you see this piece of core and this particular statement here by looking at this type you are casting it to mobile that's why you're writing mobile over here and also you can store this particular object the casting super type in which class you are casting here mobile class right who is the super type of verbal class if you talk about over here there is no super class for mobile but edge we know all the classes in Java got the super class which is object so I can store with this mobile here in mobile because this and this need to be matched otherwise I can store it with it with object type right that's also fine ok so if I'm going to run this particular program so Java C used mobile dude Java you can see this compiles fine right so Java is not complaining about it because the thing is the subtype can be store inside the super class reference and this S is Samsung type and object is the super class of Samsung right I'm casting to mobile that means I do have a another super class for this mobile which is object ok so this class is the sub class for this and this class is the sub class for the object okay so I can store it over here now just tell me one thing if I'm going to do mobile dot calling so right now let's just tell me either I'm going to get a compilation error or this particular program is going to work fine okay so let's apply our rule so I told you this Mobile is a public type and this object this particular class has been written by the Sun Microsystems people and those people does not have a method call calling inside our object class in our object class we do have eleven methods right and we do not have any methods said calling right we we just have created this particular matter and this calling method is available inside the mobile class so whatever the reference that we have for the parent type we are only able to call those specific methods so here the calling method is not available in the object class so right now if I am going to compile this particular program again you can see okay let me save this and let me do just a moment let me save this again and let me run it and I'm getting an error here is a saying cannot find symbol this calling method is not there in the mobile type because the mobile is belongs to the object right but if I am going to change it to the mobile type now it's gonna work fine because this calling method is present in the mobile type okay because I'm using the mobile reference here alright so that's a another thing that you have to remember okay so there another thing so whatever we learnt over here we can store it either in mobile because this is mobile and this is mobile here or we can store it with object type which is the super class of this mobile but we cannot try to store it in the subclass variable okay so if I'm trying to store it with some different class variable which is not the superclass of this mobile at that time we are going to get compiled as an error saying incompatible type okay so if I'm going to compile this particular program you can see I'm getting an error okay Samsung mobile okay probably I spelled it wrong okay this I put some some okay Samsung I'm sorry and here if I'm going to run this yeah there we go incompatible types right mobile cannot be converted to Samsung so because here I am trying to convert it to mobile so this should be mobile but I am trying to write it over here Samsung so this mobile cannot be converted to Samsung because this need to be store in the superclass object okay in the superclass reference you can say okay and Samsung is the subclass it's not the superclass of mobile right so this and this does not match and this is not the superclass of this all right okay one more thing that I want to tell you before we go for our exercise session I will just tell you one more thing here see here we are casting this s to mobile right we are casting this is which is Samsung class object to mobile right so the reason why we can cast it because of this extends QR this Samsung and this mobile does have some relationship and if they have the relationship at that time only we can we can cast okay if this thing does not have any relationship we cannot cast them okay for an example right now I am removing this extends mobile from here so this is a different class and this became a different class they do not have any relation if they do not have any relation first of all this particular program is not going to compile because I am trying to create this Samsung object over here so s is some some type and I am trying to cast this edge to mobile so this mobile and this s s object or s reference which assumption they do not have any relationship okay so if they do not have any relationship you cannot cast it okay you will have compiled as an error right so let me save it and if I will compile this we are going to get compliation error saying cannot convert to mobile to Samsung right okay so these are the few things that I want to tell you before we go for our exercise session okay so right now we will do some exercise here and what you need to do you just need to guess the output okay so let me take you to my whiteboard we are going to just play around with some code okay based on of casting okay so let's go for it all right so let's do some exercise right now okay so now let's do some exercise okay so I hope you already guys have some fundamentals what up casting is so let's do something really really quick okay I need the answer from you guys just just guess it with me okay whenever I'm writing some code it just gets output okay so just think like I have a class called a in the in the a class I have a method called m1 I have a class called B which exchange to a and in the big class I a method called m2 and I have a class called C and in the C class this C class is also extending to the big class and in the C class I have a method called m3 okay so now let's say if I want to store this C class object in the big class how can I do it okay so I want to store this C object in the B type reference right so let's create the C class object first so let's say C c1 equal to nu C okay and right now I am just casting this c1 object to a super type which is B so I have casted it to the precipitate so right now I have to store it in a reference let's say obj and this need to same as this so be I'm casting it to P so I'm writing over here B and I can also store so this and this need to be match otherwise this need to be the superclass of this be right so I can also write with the superclass of B as the superclass of P I can write a here right so a is the superclass of B so here it is B so this need to be same as this that these two need to be same otherwise this need to be the superclass of B so a is the superclass of B so I can happily write it otherwise you know above a we have object class right object class is by default the superclass of this particular a right so I can also write object here okay so this is also fine right object obj equal to I'm casting c1 to be okay that's also fine but right now let's go it's simple let's make it be okay so b and b is matching right okay so right now I can say obj dot m1 is it valid or not just check it out over here it's obvious there ends to with but which particular type B type this obj is B type right in the in the B we had been one method or not okay so right now the iron one method is not over here but this big glass is extending to a class so by default this m1 method is present over here so this particular code is valid rights of obj toward m1 we can do it happily right so right now and I do Obie's a dot m to just think and say write this m2 is present over here obj is what type it is B type right so in the B type m2 method is present over here or not yes it is present over here then the m2 method will be get called no problem right now I will do obj of m3 okay so this particular line of code is going to be correct or it is going to be give it to me a compiled list an error this thing like this all B's a this.o B's are here of what type this is a bit type in the B type we have been three method we have been three method here no if the n3 method is not here then we are going to get compilation error we are going to get a compilation error and this particular code will not be get executed the reason for this this obj is a B type and in the B we do not have the n3 method okay so you have to understand this as I said okay so actually we're storing the c1 the subtype in the super type object which is P we are going like this so this is called a casting okay so right now let's say I want to store this three class object in the in the a class reference so with that with a a one line how can I write it I can I want to store the C class subject shall write nu C here and I want to store it in the a class reference so right I'll write a and a right this is done this is what we are doing of casting and storing C plus object in the a class I mean the in the air type reference right I'm storing this see in the a I'm going up right so I'm doing up casting right so can I do a dot M one okay so this M one method is over here yes it is there then it is going to work fine this is going to compile fine this particular method will be get called so right now can I do at em - okay so m2 method is here into a claw indirect in the A+ no it is not there so this particular code is going to give me compliation error because this a is of a type in the end the a class we do not have a method called m2 so it is going to give me compilation error okay so I can i can store B in the a right so B is the subtype is the super type can I write like this I want to store the be here and I want to store it with the a class reference is it possible yes this absolutely valid this is what we're doing up casting I'm telling the big class object in the a class reference so I'm storing this object in the a class reference I'm going up so this is what we're doing of casting so can I do a dot M one phase of what class reference I'm in what class reference a class reference in the right class we have been one method yes so it is going to bail it right and I do a dot high score so this has code method is present where this has code method is present in the object class so all the classes this a class is by default extending to the object class so the object class is the superclass of all the classes in Java right and in the object class we have a method called has code right so if hashcode method is present over here this method is by default coming to this particular class because this is extending to this so entered has code is going to work and not obviously it is going to work ok can I do a dot M 3 so n 3 method is present we're in the c class so a is whatever reference a type reference we do not have been three measured over here so this is going to give you compilation error simple right okay so one last thing okay suppose I want to store this big class reference okay okay so okay so let let me do it manually okay I want to store the big class reference in a super type so I'm creating the b-class object here B be equal to MU B okay now I want to cast this B to a super type let's say a okay so k9 and I right here object Oh it is valid or not so as I said this and this need to match right now is it matching no this is a this is object this is not matching this a different class this is a different class okay so if this is not missing is this particular class is the superclass of this air class superclass is what of the class so can I write object here yes I can able to write object here okay so can I do Oh dot m1 here now you have to think okay this all belongs to object class in the object class have you have you written this object class no so we have written this object class Sun Microsystems people has written this object class and inside this object class do we have a method called hi I mean do we have a method called m1 no this m1 is our method right we have written written it in the a class so in the object class we do not have a method called m1 here so this is going to give me compile as an error because this all belongs to object type okay this is this all is object type all right so this is going to give me an compilation error so the thing you have to remember this reference if you are if you are taking the parent class reference this is limited right whatever the things is there in in that particular super type or in that particular parent class only those method you can call right so we have done it in the entire I mean the entire thing that we have done over here we have just understood this thing right okay one last thing okay before we wrap up this particular session okay so let me remove this piece of code here okay so if let's say I want to store the c-class object okay let's say C c1 equal to nu C now I want to cast this say one object into the type no problem so right now I am so I also right now I'm storing it in the C Class difference okay this thing this piece of code is valid or not so this B and this is C so this is B and this is C this too is not matching if not missing this C must be the superclass of be okay the C must be the superclass of B so right now does C is the super class of B no this is the subclass so this is invalid right either it need to be B or it need to be a superclass maybe a maybe object or something okay but I'm writing C here right so if you are doing this kind of coding you are going to get compilation error okay saying incompatible type right this B need to be store in B type or in or a super type otherwise you'll get incompatible error you know incompatible compilation error okay and one more thing which is very important okay let me tell you that also okay this think like if this B is not extending to it okay if this B is not extending to a and right now I told you okay just think like I have only two class here a and B okay let me remove everything so that you guys will not be confused okay so right now this is a different class and this is a different class this class is not extending to this class okay so can I can I do this B b1 equal to nu B okay this is this class reference so can I cast this b12 is Lester a type and I'm storing a a one so is this a valid piece of court you have to check this and this is not this there is no relationship right this is not extending to this particular class and if this is the case if this be one which is this big big class object reference and this a class does not this be one is a big class reference right so if this B and a does not have any relationship this is not going to be compiled at all all right so the compliation is going to be felt I mean you are going to get a comparison error saying in convertible in convertible type or something probably okay in convertible type right the stake over here okay I'll just write it down over here okay so this make sure that this thing is not going to be compiled because this and this need to be in a relationship if we are casting them okay so simple right so this is the entire thing that I want to tell you about the odd casting and so that's it for today and in the next tutorial we are going to talk about down casting in Java okay so the thing is actually if you talk about this kind of thing using a you can only call this method which is available in the a-class right you can only call the A+ method using this reference so what about if you want to call this a class method okay so how can we do it we can do it by using down casting and that's what we are going to talk about in the next tutorial and yes if you are talking about the advantages of a up casting what what kind of advantages that we are getting if you are using this this kind of code we are going to talk about it in the next next tutorial that means in the third tutorial okay so don't forget to subscribe to my channel to get all these videos and I'll see you in the next video thank you very much guys for watching do subscribe do share if you like this tutorial if you watched the entire tutorial if you're finding it could then do share it with your friends with with a colleague or whoever you want to say okay so thank you very much guys for watching it hope you'll have a wonderful day take care and happy coding [Music]
Info
Channel: Selenium Express
Views: 32,588
Rating: 4.8808703 out of 5
Keywords: upcasting and downcasting in java, object casting in java, casting objects in java, casting in java, runtime polymorphism in java, upcasting in java, upcasting in java with examples, parent p = new child(), inheritance in java with example, dynamic dispatch in java, inheritance in java in english, parent class reference child class object java, downcasting in java with examples, selenium express, java oops concepts with examples, super class sub class java
Id: vp6Mcxc1CTM
Channel Id: undefined
Length: 48min 51sec (2931 seconds)
Published: Sun Apr 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.