1

00:00:01,110  -->  00:00:02,310
Hi there.

2

00:00:02,570  -->  00:00:07,490
Now let's look at an interesting property of inheritance which is called us constructor chaining.

3

00:00:07,740  -->  00:00:13,710
And at the end of the lesson we will also do a more off concept touching House let's look at the motivation

4

00:00:13,740  -->  00:00:15,640
for construct searching.

5

00:00:16,230  -->  00:00:23,850
We know that subclauses inherit methods from superclasses inducement that might depend on supercluster

6

00:00:23,850  -->  00:00:24,480
.

7

00:00:24,480  -->  00:00:29,450
That is the instance variables that are defined and superclasses.

8

00:00:29,450  -->  00:00:33,290
So it means that supercluster it should first be initialized.

9

00:00:33,420  -->  00:00:39,840
Otherwise you may create an object and when you invoke an inherited method it might not work as expected

10

00:00:40,280  -->  00:00:44,000
as a state in the superclass is not yet initialized.

11

00:00:44,100  -->  00:00:47,800
So supercluster it should be initialized first.

12

00:00:47,820  -->  00:00:54,180
Similarly a superclass might have another superclass and consequently it should also be initialized

13

00:00:54,390  -->  00:00:56,610
even before the period superclass.

14

00:00:57,120  -->  00:01:02,910
So basically is subclassed it should be initialized after all its superclass states are initialized

15

00:01:02,910  -->  00:01:03,330
.

16

00:01:03,630  -->  00:01:09,350
And that would include the object class to which is a top most superclass.

17

00:01:10,230  -->  00:01:13,140
And below other constructors are used to initialized it.

18

00:01:13,520  -->  00:01:14,860
And what does that mean.

19

00:01:14,880  -->  00:01:20,810
It means that all the superclass constructors should run before the subclass constructor.

20

00:01:21,280  -->  00:01:24,480
And this is what constructor chaining does.

21

00:01:24,540  -->  00:01:28,700
We know that when we are creating an object it just runs to construct of code.

22

00:01:29,090  -->  00:01:35,760
But duplicating the first thing that the constructor does is do in work its superclass constructor which

23

00:01:35,760  -->  00:01:38,760
in turn calls its own superclass constructor.

24

00:01:38,760  -->  00:01:45,000
And this process continues on the class objects construct resident and this concept of training process

25

00:01:45,240  -->  00:01:49,190
happens every single time you create a new object.

26

00:01:49,200  -->  00:01:52,130
Let's go ahead and look at an example.

27

00:01:52,470  -->  00:01:58,150
Let's assume we are creating a staff object then the object creation statement would invoke that stops

28

00:01:58,170  -->  00:02:05,870
constructor unstopped constructor would immediately invoke it superclasses constructor which is the

29

00:02:05,890  -->  00:02:12,810
user's constructor and then uses constructor would then immediately invoke object classes constructor

30

00:02:13,380  -->  00:02:14,640
as object as the superclass.

31

00:02:14,670  -->  00:02:18,690
Off user ones object constructor is run.

32

00:02:18,860  -->  00:02:25,380
Execution goes back to user constructor and it then picks up at the line following the users in will

33

00:02:25,380  -->  00:02:28,480
Keeshan to its superclass constructor.

34

00:02:28,500  -->  00:02:34,050
Similarly once users constructor is completed then the remaining code and stops constructor is executed

35

00:02:34,050  -->  00:02:35,770
.

36

00:02:35,960  -->  00:02:39,670
Nobody saw that to invoke superclass methods or hidden fees.

37

00:02:39,690  -->  00:02:42,390
They could use Superguy work.

38

00:02:42,630  -->  00:02:50,370
Similarly we can use Superguy word who in work is superclass constructor fathers the Superguy word would

39

00:02:50,370  -->  00:02:57,380
be followed by parenthesis which mean how zero or more arguments Aspen's kept us going how barometers

40

00:02:57,380  -->  00:02:58,900
.

41

00:02:58,920  -->  00:03:05,760
The important thing is the super invocation statement must be a statement in the constructor unless

42

00:03:05,910  -->  00:03:08,730
at this invocation statement is used.

43

00:03:09,190  -->  00:03:10,110
Because that doesn't work.

44

00:03:10,110  -->  00:03:16,350
That statement is useful in working order or that constructors must also be the first statement in the

45

00:03:16,350  -->  00:03:18,450
constructor.

46

00:03:18,450  -->  00:03:23,650
So the constructor can how either this in location statement autists opening statement.

47

00:03:23,820  -->  00:03:26,130
But never bought.

48

00:03:26,490  -->  00:03:32,250
So if there are multiple overloaded constructors and if they happen to invoke each other than the last

49

00:03:32,280  -->  00:03:38,100
invoked constructor would be responsible for smoking its superclass constructor.

50

00:03:38,100  -->  00:03:42,870
So for sure there'll be at least one constructor in a class which does not work.

51

00:03:42,930  -->  00:03:50,360
Another overloaded constructor that will have a place where invoking the superclass constructor shouldn't

52

00:03:50,370  -->  00:03:53,160
provide a super and location statement than the compiler.

53

00:03:53,210  -->  00:03:56,980
So I'd say no odd super invocation statement.

54

00:03:57,090  -->  00:04:03,210
Again this will be done in the constructor that does not invoke an overloaded constructor.

55

00:04:03,480  -->  00:04:08,940
So if you consider this use a class which does not have a super and location statement then upon completion

56

00:04:09,450  -->  00:04:14,290
compiler would add such a super invocation statement as the Fosdike.

57

00:04:14,580  -->  00:04:18,400
You may also recall that compiler and so it's a not constructor.

58

00:04:18,480  -->  00:04:25,350
If a constructor is not provided with the constructor actually includes Enno are putting Will Kishan

59

00:04:25,350  -->  00:04:27,030
statement.

60

00:04:27,480  -->  00:04:32,970
So if you consider this use it class this does not have a constructor then the compiler would add such

61

00:04:32,970  -->  00:04:38,640
a nawt constructor which includes a super and location statement.

62

00:04:38,640  -->  00:04:45,090
Now if for any reason a superclass constructor cannot be invoked then a compiler error is generated

63

00:04:45,090  -->  00:04:46,670
.

64

00:04:46,670  -->  00:04:47,950
Here is one example.

65

00:04:48,030  -->  00:04:52,420
Here we now User class Vittert constructor that takes a single parameter.

66

00:04:52,440  -->  00:04:58,090
Similarly stuff which is extending user also has a constructor that picks a single parameter.

67

00:04:58,300  -->  00:05:01,680
Not since but use it on stuff classes already have constructors.

68

00:05:01,740  -->  00:05:08,940
Compiler will not insert the default nor constructors on this code will give a compiler error because

69

00:05:08,940  -->  00:05:16,860
compiler will not be able to add in are super in location statement within the staff's constructor as

70

00:05:16,860  -->  00:05:19,770
there is not before constructor in the User class.

71

00:05:20,070  -->  00:05:23,040
So there are two things we can do to fix this.

72

00:05:23,130  -->  00:05:28,440
One is we can write a statement in Wilgus superclass constructor.

73

00:05:28,440  -->  00:05:34,590
Alternatively we can add a default constructor in the User class and this would allow a compiler to

74

00:05:34,650  -->  00:05:39,640
insert and no super invocation statement in stuff class.

75

00:05:39,750  -->  00:05:46,080
So the point is that superclass constructor should be in someway either the programmers should do it

76

00:05:46,350  -->  00:05:48,970
or the compiler should be able to do it.

77

00:05:48,990  -->  00:05:51,240
Otherwise you get a compiler error.

78

00:05:51,780  -->  00:05:53,620
So that's a constructor chaining.

79

00:05:53,790  -->  00:05:55,350
Let's not do a quick demo.

80

00:05:55,440  -->  00:06:01,090
Best construct arching could a monster constructor chaining.

81

00:06:01,110  -->  00:06:09,230
I added some code so I did some Corden's got Java as well as a user not Jela unstaffed or Java.

82

00:06:09,240  -->  00:06:14,850
We have a concept of how a default constructor which is from one of the previous most and all it is

83

00:06:14,850  -->  00:06:21,450
doing is re-assigning the user type variable which is defined in User class to stuff and then I just

84

00:06:21,450  -->  00:06:26,430
introduced this print statement which has been doing stuff constructor run and then I added this new

85

00:06:26,430  -->  00:06:33,660
constructor an overloaded constructor calls with this stuff and it has a single parameter ID and the

86

00:06:33,660  -->  00:06:39,570
first statement is invoking the default constructor which is an overloaded constructor so it's invoking

87

00:06:39,570  -->  00:06:46,830
the default constructor and it is re-assigning the IAB variable to ID it Hardie's an instance variable

88

00:06:46,830  -->  00:06:55,070
which is defined here and it's employment's this text stuff constructor to an end user and or Java.

89

00:06:55,080  -->  00:06:57,390
Also added a new constructor.

90

00:06:57,570  -->  00:07:04,050
It's a default constructor and all it does is it prints this text to use a constructor.

91

00:07:04,290  -->  00:07:06,220
Now end user test.

92

00:07:06,240  -->  00:07:12,370
All I'm doing is simply creating an instance of stuff and passing this value treat.

93

00:07:12,660  -->  00:07:15,790
So basically I'm invoking this constructor.

94

00:07:15,960  -->  00:07:22,800
This new constructor here and 3 is getting passed here so this is a constructor which is getting invoked

95

00:07:23,010  -->  00:07:30,240
and but then it will invoke the default constructor in staff and compiler would automatically insert

96

00:07:30,780  -->  00:07:36,750
the super invocation statement here as we've discussed and so the constructor Newser class would be

97

00:07:36,750  -->  00:07:37,770
invoked.

98

00:07:37,770  -->  00:07:44,130
So this statement should be the first one that gets printed and then after that the control comes back

99

00:07:44,700  -->  00:07:51,330
to here and user types the user gets reassigned to staff and then it prints stuff constructor run after

100

00:07:51,330  -->  00:07:55,310
that control comes back here to this statement.

101

00:07:55,330  -->  00:08:00,400
Actually this statement onic reasons ID and then it been stuffed constructor too.

102

00:08:00,570  -->  00:08:03,560
So let's just go ahead and just run this.

103

00:08:04,080  -->  00:08:09,600
As you can see the last statement that got brain dead was to use a constructor then Stav constructor

104

00:08:09,600  -->  00:08:13,220
one unstaffed constructor too so the new action happened this way.

105

00:08:13,230  -->  00:08:19,930
But then you know it got brain dead and disappeared so that's all because our constructor chaining.

106

00:08:19,980  -->  00:08:26,370
So we invoked this particular constructor first but the statement here got brain dead last.

107

00:08:26,600  -->  00:08:31,640
OK so that's because of constructor chaining and that's about it.

108

00:08:31,740  -->  00:08:32,820
The constructor chain
