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