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