Not let's look at casting off objects or after discussing casting they will also look at a new operator 2
card instance or we look at typecasting during our discussion on primary variables. 3
It allowed us to assign primitive variables all the close of one by two variables off another type. 4
And it's because both implicit as well as explicit casting implicit casting is done when something smaller 5
is assigned to a larger variable unexposed casting is needed when something larger is assigned to a 6
smaller variable. 7
Just recall that typecasting was applicable only if both data types but often your Model X that is any 8
good or type other than Boolean. 9
So by casting what's needed as one pyramided variable to run would be assigned to another variable and 10
we saw that with polymorphism we can now assign variables of 1 class two variables of another class 11
. 12
And we also need gasping here. 13
Now we looked at this example when introducing polymorphism. 14
In fact it is a very definition of polymorphism of polymorphism. 15
We are able to pass a subclass object as argument with a method parameter which is off by superclass 16
. 17
So basically what we are seeing here is implicit casting in action. 18
That is a separate object is implicitly cast it with a superclass variable. 19
But once implicit casting is done VIGEN only in work DOS subclass methods which are also defined in 20
the superclass. 21
That is we cannot in any method that are specific to subclass were discarded when introducing polymorphism 22
. 23
And here is an example cross implicit cast is done to assign editor object to a gray area. 24
Will I stop you know that editor is it some pipe office. 25
Once this is done we can no longer invoke any methods that are specific to Ed. And we know that this 26
is because compiler uses referenced type to decide on whether meathook can be involved or not. 27
So here we get compiler error as the reference type stuff does not how it would have matter to exist 28
. 29
We need to insert a digger as an explicit cast as shown here. 30
Now we are promising compiler that at runtime the variable s been referencing an editor object or one 31
off it's a process in invoke the method we need to parse or parenthesis one with the cast itself on 32
the other on the cast and a variable. 33
Now this example is same as the previous one but it is ex-POWs in a more realistic way. 34
Here the matter a prove review has staff as input parameter and it in turn will prove to be a third 35
on the input stuff object which we know gives a compiler and do what does we insert editor as an explicit 36
ghast. 37
So as mentioned earlier by inserting this ghast we are promising the compiler that at runtime the variable 38
as well be referencing an editor object. 39
Now see if this method is invoked with a stop object that is we are breaking the promise we made to 40
the compiler. 41
Now with this we get an error at runtime and this error is called Class Guste exception. 42
That is the input object and not because it a specific class that is. 43
And this gives the staff object cannot be trusted to editor and this is because staff can be an editor 44
but it can be something else too. 45
For instance it can be an email admin too. 46
So while it is a staff a staff need not be editor. 47
And so we get this error at runtime. 48
Now one thing we can do. 49
What does run by matter is to be a logical test before doing the cast on product. 50
We could use something called an instance of operator. 51
And here's the syntax on the left. 52
You have an object reference on that right. 53
We have the name of the class instance of operator operate in a group. 54
If the object referenced by the object referenced on the left is indeed an instance of a class on the 55
right not that it is the object references object not the reference type is what matters. 56
So just take that in mind. 57
Also the object referenced by the Brady bill can be a subclass of the class specified on the right side 58
. 59
So it doesn't have to be a direct instance not just look at some instance of examples let's consider 60
this through declarations just one second instantiate user On-Star classes. 61
Well the third one creates an instance of staff but the reference type is user. 62
And this example is related to the declaration since a variable use object type is user it is an instance 63
of user. 64
And hence it truly is written in this case. 65
This expression returns false as use object type is user which is not an instance of stuff. 66
And we know that stuff is a subtype of user. 67
And this is related to the second declaration here. 68
Object type or variable. 69
As is stuff. 70
So this evaluates to true this would also evaluate to true as sort of rinsing as stuff objects and stuff 71
. 72
Is this a type of user. 73
So as we mentioned earlier the object reference can be a subclass of the class on the right side of 74
the instance of operator like in the previous example. 75
This also evaluates to true as the variable us in the third declaration is referencing stuff object 76
type which is a subclass of user. 77
And the final example also evaluates to true as it is the object type that matters. 78
And here is a new rule review mattered which prevents the potential runtime error. 79
It does if the input variable as is an instance of editor. 80
Only if the test succeeds. 81
It applies to cast on in walks not approved method. 82
So we cannot use these kinds of invocation statements. 83
The test place for the frost in location where a staff object is being passed us argument until the 84
run parameter is presenter. 85
But the best succeeds for both the second and even the third in location where Chief Editor object is 86
passed and we know that Chief Editor is a subtype of editor. 87
Now let's go ahead and look at all of the stuff we learned in court. 88
This is a prove review method and it has been added to the user test class which has been created in 89
the previous demo. 90
And here is the final logic that we have seen in the slide. 91
But I'm going to start with the demand starting from the beginning the concepts that we have started 92
learning from the beginning of the lesson. 93
So initially we were in walking it prove review method. 94
So the input we able to this method is staff and be ready in working a prove review. 95
And it was a compilation error because the compiler knows that a proper review method is not in the 96
staff class. 97
So we said the compiler looks at the reference with you but not the object but the reference variable 98
. 99
And in this case a reference variable is staff and it doesn't have the approval of your mentor. 100
So we get a compiler error when I holler over here it says I'm at a poor review is undefined. 101
So let me just comment this out and the solution was to insert a cast. 102
And so here we are applying this cast we are inserting this editor as a cast. 103
I know it works. 104
So we don't see the competition either. 105
So let's just run this. 106
So we are in working we have created in an instance of the user base class and we are invoking the approval 107
of your method but we are invoking word by passing staff object Asom motard argument. 108
So this would give us a runtime error. 109
So when I run this as we discussed it is doing class cost exception. 110
So that's a runtime error that we're getting here. 111
So it's no longer it's not a compilation error but it's a runtime error on says that the error is happening 112
at line 11. 113
So this is where the casket is being applied. 114
And we were passing a Stav object instead of passing an editor object or one of its subtypes. 115
We promised the compiler that we would be passing either an editor object or one off its up. 116
And we did not do that. 117
And so we are getting this runtime error. 118
So now to avoid this we said we can use instance of operator and we did that and this is the logic. 119
So let me just uncommon discord. 120
So here we are applying the instance of operator and if it is really an instance of a date or one off 121
it's subclasses then we were in Roeg this method we would apply the cast on in this. 122
Otherwise we would simply print this statement invalid object passed. 123
So now if we invoke this it is invalid object passed when we are passing stuff. 124
So let me comment this on that we pass an editor object. 125
So and I run this it's this editor a pro what have you. 126
So it's actually invoking a prove review method in editor class. 127
So. 128
So it is going here and it is invoking a proper review method and the reader class. 129
So probably you are and it is going to print this statement. 130
This this text. 131
So that's about it. 132
And once again I'm showing this a gym or an eclipse to sip some time. 133
And you you know you can download discorporate in the resources section and you can buy it in your or 134
whatever editor you are using on you contested