1
00:00:01,240 --> 00:00:05,800
We've already established in part one that there's going to be some inheritance involved, more specifically

2
00:00:05,800 --> 00:00:07,810
if you go back to your requirements.

3
00:00:08,170 --> 00:00:13,810
You can see that checking savings and loan share common fields and name in balance.

4
00:00:14,560 --> 00:00:17,840
And it doesn't make any sense to keep defining the same fields over and over.

5
00:00:17,870 --> 00:00:20,590
That's when you're going to find yourself copying and pasting code.

6
00:00:20,890 --> 00:00:23,140
And you know my thoughts on that, never do it.

7
00:00:23,590 --> 00:00:29,270
Instead, every account is going to inherit fields from a parent class, so I'm going to put an account

8
00:00:29,410 --> 00:00:31,660
in Java file inside of the account folder.

9
00:00:36,640 --> 00:00:41,050
All right, in the count class is going to serve as the parent for checking savings and loan.

10
00:00:41,530 --> 00:00:43,780
Once again, going back to the requirements.

11
00:00:45,030 --> 00:00:50,520
The developer is only permitted to create an object of type checking savings and loan.

12
00:00:51,120 --> 00:00:54,360
They are not permitted to create an object of the account class.

13
00:00:54,720 --> 00:00:56,790
So what we can do is make this abstract.

14
00:00:58,880 --> 00:01:03,860
And what it's going to do is basically forbid the developer from creating an object of the account class.

15
00:01:04,099 --> 00:01:07,850
The only purpose of account is to provide inheritance for its children.

16
00:01:08,600 --> 00:01:12,260
And we also know every account needs to inherit a string ID.

17
00:01:13,220 --> 00:01:14,780
Now make sure it's private.

18
00:01:16,860 --> 00:01:18,000
Private string name.

19
00:01:20,260 --> 00:01:23,050
And a private double balance.

20
00:01:25,260 --> 00:01:27,900
All right, now, we can create a constructor for the parent class.

21
00:01:29,400 --> 00:01:30,750
Public accounts.

22
00:01:32,600 --> 00:01:33,410
Actually, you know what?

23
00:01:33,440 --> 00:01:36,440
We can just use the Java code generator extension.

24
00:01:37,480 --> 00:01:44,440
We will generate a constructor using all fields, and we can do the same thing for the copy constructor,

25
00:01:44,440 --> 00:01:49,000
but instead of getting values from a bunch of parameters, we're going to get the values from a source

26
00:01:49,000 --> 00:01:56,560
object, the object that we're copying values from sorry start IDs or start name, sorry start balance.

27
00:01:57,310 --> 00:02:01,300
Now we could do the getters and setters again, just generate them using the extension.

28
00:02:01,750 --> 00:02:02,650
Pretty easy.

29
00:02:04,110 --> 00:02:04,420
All right.

30
00:02:04,440 --> 00:02:08,880
And now that we've set up the parent class, every child is going to inherit the fields and methods

31
00:02:08,880 --> 00:02:10,289
that we defined in our parents.

32
00:02:11,009 --> 00:02:16,080
And so checking is going to inherit the fields and methods from the account class.

33
00:02:16,080 --> 00:02:18,120
So it's going to extend account.

34
00:02:21,200 --> 00:02:25,160
Savings is also going to inherit fields and methods from the account class.

35
00:02:26,900 --> 00:02:31,790
And so will the alone account class extends accounts.

36
00:02:36,640 --> 00:02:41,290
And now we're getting errors because every child needs to have a constructor that synchronizes with

37
00:02:41,290 --> 00:02:42,520
the parents constructor.

38
00:02:43,120 --> 00:02:47,740
So we're going to create a constructor instead of checking public checking.

39
00:02:50,180 --> 00:02:53,090
The constructor is going to receive three parameters a string ID.

40
00:02:54,290 --> 00:02:57,500
String name and double balance.

41
00:02:58,750 --> 00:03:04,120
And we're going to use these parameters to update the fields that our checking object is going to inherit.

42
00:03:04,510 --> 00:03:07,540
So our constructor needs to borrow the super constructor.

43
00:03:10,980 --> 00:03:17,070
Basically, this line of code is calling the constructor inside of the parent class, and that constructor

44
00:03:17,070 --> 00:03:20,190
is going to update every field inside of the current object.

45
00:03:23,730 --> 00:03:25,500
All right now, create a copy, constructor.

46
00:03:26,280 --> 00:03:27,450
I'll make some space here.

47
00:03:28,500 --> 00:03:30,180
Public checking.

48
00:03:31,330 --> 00:03:34,270
That's going to receive a source object checking.

49
00:03:35,480 --> 00:03:36,190
Source.

50
00:03:36,830 --> 00:03:41,900
And once again at this constructor needs to borrow the copy constructor inside of its parent.

51
00:03:46,620 --> 00:03:50,910
This line of code is calling the carpet constructor inside of the parent class.

52
00:03:51,660 --> 00:03:55,890
And it's this constructor that's going to update every field inside of the current object.

53
00:03:59,560 --> 00:04:05,960
OK, now we basically do the exact same thing inside a savings critic instructor, public savings.

54
00:04:07,580 --> 00:04:09,840
And the constructor is going to receive three parameters.

55
00:04:09,860 --> 00:04:10,520
String ID.

56
00:04:11,760 --> 00:04:12,570
String name.

57
00:04:14,170 --> 00:04:15,940
And double balance.

58
00:04:17,470 --> 00:04:21,970
And we're going to use these parameters to update the fields that are receiving the object is going

59
00:04:21,970 --> 00:04:22,690
to inherit.

60
00:04:23,080 --> 00:04:25,810
So our constructor needs to borrow the super constructor.

61
00:04:31,520 --> 00:04:37,910
Nail credit, carpet constructor, public savings and the constructor is going to receive a source object

62
00:04:37,910 --> 00:04:40,520
savings source.

63
00:04:41,060 --> 00:04:47,420
And once again, this constructor needs to borrow the copy constructor inside of its parent super source.

64
00:04:48,770 --> 00:04:49,100
OK.

65
00:04:51,020 --> 00:04:53,030
We'll do the exact same thing inside of loan.

66
00:04:53,570 --> 00:04:57,590
Public loan, though, receives three parameters.

67
00:04:58,250 --> 00:05:02,030
When we create a loan object, it needs an ID, a name and a balance.

68
00:05:03,240 --> 00:05:08,970
And we're going to pass those values into the super constructor, and it's the super constructor that's

69
00:05:08,970 --> 00:05:11,820
going to beat every field inside of the current object.

70
00:05:12,690 --> 00:05:14,100
Same thing for the copy constructor.

71
00:05:16,560 --> 00:05:19,640
And the constructor is going to receive a source object.

72
00:05:21,370 --> 00:05:27,100
And once again, the scarper constructor needs to borrow the constructor inside of its parent, and

73
00:05:27,100 --> 00:05:31,540
it's the parent constructor that's going to update every single value inside of the current object.

74
00:05:31,990 --> 00:05:33,160
And that is wonderful.

75
00:05:33,160 --> 00:05:37,750
Now, every single child class is capable of upgrading the fields that it inherits.

76
00:05:42,530 --> 00:05:47,540
What we're going to do is test our code, simply copy these objects from learn the parts.

77
00:05:55,000 --> 00:05:55,490
All right.

78
00:05:55,510 --> 00:05:57,880
We should get some areas that's fine.

79
00:05:59,030 --> 00:06:01,020
We just have to import the classes.

80
00:06:01,040 --> 00:06:02,600
We can just use auto complete.

81
00:06:06,000 --> 00:06:06,930
And.

82
00:06:08,540 --> 00:06:09,550
Should be get to go.

83
00:06:12,790 --> 00:06:14,470
I'll just add three break points.

84
00:06:15,900 --> 00:06:17,370
And we'll debug the time.

85
00:06:33,640 --> 00:06:39,880
So here we're creating an object of the chugging class, the object inherits three fields balance ID

86
00:06:40,000 --> 00:06:40,510
and name.

87
00:06:42,520 --> 00:06:44,560
Here we're borrowing the parent constructor.

88
00:06:45,480 --> 00:06:49,570
And what it does, is it upbeat every single field inside of the current object?

89
00:06:49,590 --> 00:06:51,570
This the object that was just created.

90
00:06:55,830 --> 00:06:56,340
All right.

91
00:06:58,540 --> 00:07:04,360
And here we're creating an object of the savings class, the savings object inherits three fields balance

92
00:07:04,360 --> 00:07:05,560
ID and name.

93
00:07:06,250 --> 00:07:10,540
And here we're borrowing the parent constructor that's going to update every single field inside of

94
00:07:10,540 --> 00:07:11,680
the current object.

95
00:07:12,130 --> 00:07:12,790
This.

96
00:07:16,160 --> 00:07:18,350
All right, everything is looking good so far.

97
00:07:21,380 --> 00:07:23,750
Error creating an object of the loan class.

98
00:07:25,090 --> 00:07:27,310
The constructor receives three parameters.

99
00:07:27,400 --> 00:07:33,520
String ID, string name, double balance, we're passing these parameters into the parent constructor,

100
00:07:34,750 --> 00:07:39,130
and the parent constructor is going to update every single field inside of the current object.

101
00:07:43,100 --> 00:07:44,090
All right.

102
00:07:44,810 --> 00:07:49,490
And we're left with three account objects checking savings and loan.

103
00:07:51,910 --> 00:07:57,610
This is the beauty of inheritance, we only had to define this constructor and this can't be constructor

104
00:07:57,610 --> 00:08:03,880
once, and we borrowed it inside of every single child class instead of redefining the same thing over

105
00:08:03,880 --> 00:08:04,330
and over.

106
00:08:04,660 --> 00:08:07,750
That's why inheritance promotes code reusability.

107
00:08:08,620 --> 00:08:09,340
All right.

108
00:08:10,060 --> 00:08:14,580
So we just confirmed that each child is using the parents constructor to in its fields.

109
00:08:14,590 --> 00:08:20,410
And if you want feel free to test the copy constructor, I'm just going to move on to the final task,

110
00:08:20,800 --> 00:08:24,610
which is to going back on learning the part.

111
00:08:24,610 --> 00:08:26,720
We need to add this to string method.

112
00:08:27,520 --> 00:08:32,049
And now you might think to add a tutoring method inside of each class, like checking savings and loan.

113
00:08:32,559 --> 00:08:38,049
So here inside checking, you might think to say you might think to just override you string copy over

114
00:08:38,049 --> 00:08:39,610
the format from learning the parts.

115
00:08:40,690 --> 00:08:42,940
Here we can say this stark class.

116
00:08:45,090 --> 00:08:46,710
Start, get simple name.

117
00:08:49,000 --> 00:08:50,080
Super dont get it.

118
00:08:52,300 --> 00:08:53,650
Superdog get name.

119
00:08:55,970 --> 00:08:57,740
Super don't get balance.

120
00:09:00,330 --> 00:09:06,600
And that's fine and all, but think about savings and loan, they're all just open up a tab for savings.

121
00:09:07,720 --> 00:09:08,890
Think about savings and loan.

122
00:09:09,160 --> 00:09:15,370
They share the exact same fields as checking, so you can literally just copy and paste the two string

123
00:09:15,370 --> 00:09:17,920
method for loan and savings.

124
00:09:18,340 --> 00:09:21,460
We can say we can copy this over here.

125
00:09:21,490 --> 00:09:26,440
We should get no errors because it shares the exact same fields and copy this over here.

126
00:09:27,350 --> 00:09:32,810
But you know how I feel about copying and pasting code, if you can avoid it, please do.

127
00:09:33,200 --> 00:09:39,980
Instead of redefining that seem to string method three times, every child can just inherit that method

128
00:09:39,980 --> 00:09:40,790
from its parent.

129
00:09:41,150 --> 00:09:44,450
So you can delete this method here.

130
00:09:45,260 --> 00:09:47,270
Actually, it will delete it in every single child.

131
00:09:51,950 --> 00:09:55,430
And re copy the format inside of account Dogefather.

132
00:09:59,450 --> 00:10:00,460
He will say this.

133
00:10:00,830 --> 00:10:04,010
Get class, die, get simple name.

134
00:10:06,760 --> 00:10:07,600
This start get ID.

135
00:10:10,070 --> 00:10:11,180
They start get name.

136
00:10:13,060 --> 00:10:15,400
And this does get bouts.

137
00:10:17,800 --> 00:10:21,820
We're going to be using that to string in the parent class to get the class name of the current object,

138
00:10:22,180 --> 00:10:26,260
the idea of the current object, the name of the current object and the balance.

139
00:10:27,010 --> 00:10:31,990
This is much better because now every child class automatically inherits this tutoring method.

140
00:10:32,440 --> 00:10:38,320
So back in Maine, we can test this out by just starting off with three print statements.

141
00:10:40,290 --> 00:10:41,190
Six out.

142
00:10:43,830 --> 00:10:47,190
Since it's incessant.

143
00:10:49,750 --> 00:10:52,570
And I'll just print the two string that's inherited by checking.

144
00:10:53,840 --> 00:10:56,130
Savings and loan.

145
00:11:03,610 --> 00:11:03,900
OK.

146
00:11:04,120 --> 00:11:07,960
Move these three break points at three break points right here.

147
00:11:15,260 --> 00:11:20,630
Here we can see our checking object is calling the two string method inside of its parent account.

148
00:11:21,020 --> 00:11:26,020
And to string it returns every field inside of the current object.

149
00:11:36,520 --> 00:11:37,180
Perfect.

150
00:11:40,860 --> 00:11:44,010
We'll skip over the rest and everything works out perfectly.

151
00:11:45,690 --> 00:11:49,800
All right, now, your final task was to quality control, just because you can never assume that the

152
00:11:49,800 --> 00:11:51,720
data being passed in is perfect.

153
00:11:51,960 --> 00:11:56,730
There might be something wrong, and if one of these lines ends up being faulty, you want to throw

154
00:11:56,730 --> 00:12:01,740
some kind of exception to force the caller to fix their code and handle such an event.

155
00:12:02,310 --> 00:12:03,570
So inside the constructor?

156
00:12:06,200 --> 00:12:11,750
We'll check if the ID being passed is null or a blank, if I.D. is equal to no.

157
00:12:13,420 --> 00:12:16,330
Or I'd die is blank.

158
00:12:18,320 --> 00:12:20,030
We'll check if the name is Nola Blank.

159
00:12:20,870 --> 00:12:21,890
Name is equal to no.

160
00:12:24,110 --> 00:12:27,170
Or name die is blank.

161
00:12:27,710 --> 00:12:31,610
And if one of these is true, then we need to throw an illegal argument exception.

162
00:12:32,320 --> 00:12:41,450
Throw new a legal argument exception, invalid params notice that I'm throwing the exception only once

163
00:12:41,450 --> 00:12:45,260
here instead of throwing it three different times inside of each child.

164
00:12:45,530 --> 00:12:50,630
It's much more effective to do it once inside the parent, because every child is eventually going to

165
00:12:50,630 --> 00:12:52,190
just call the parent constructor.

166
00:12:53,090 --> 00:12:56,480
Now we can quality control the Centers on Quality Control said it.

167
00:12:57,140 --> 00:12:58,610
So I'll say if.

168
00:12:59,720 --> 00:13:01,750
I.D. is equal to no.

169
00:13:02,860 --> 00:13:04,000
Or if I'd.

170
00:13:05,260 --> 00:13:05,950
Is blank.

171
00:13:07,300 --> 00:13:10,360
Throw a new illegal argument exception.

172
00:13:12,800 --> 00:13:13,640
Invalid ID.

173
00:13:16,770 --> 00:13:22,650
Same thing inside that name, if name is equal to nil or named is blank.

174
00:13:24,170 --> 00:13:28,910
There are new a legal argument exception, invalid name.

175
00:13:32,440 --> 00:13:38,110
OK, and now if the caller tries to pass on any bad values, our app is going to throw an exception,

176
00:13:38,110 --> 00:13:39,940
forcing them to improve their code.

177
00:13:41,410 --> 00:13:42,550
And that's all for part two.

178
00:13:42,580 --> 00:13:43,360
I'll see you soon.

