Heidi's rings at Howard and welcome to the next video of this object oriented programming section.
Now in the previous video we learned the concept of overriding in Gears of Cartland.
Now in this video let us check out how do we use the primary and secondary constrictors when we have
inheritance involved.
So here inside our project I have simply mortified my call.
So this is their derived class of dog.
And this is the superclass of animal.
Now notice here we have the open keyword and inside the Animal class I have the property of color.
And inside the dog I have the property of Breede.
So their dog claws indirectly gets the property variable of color as well.
So inside the main class I'm simply asserting that color as black and dog dog breed eager to pug.
So I guess no doubt they'll hear.
And now suppose instead of defining the property variable here let us use the constructor to define
the property variable.
Let's see that SPEEs breed of their type of string.
Right.
Let us know.
Remove it and now by Bideford inside the constructor as well.
You're going to find their default value.
So this is actually gunas their default constructor very refined their default values for these variables
such as I can define here black.
So if you don't pass any value inside the dog constructor then by default this really will be WILLIGER
the value of black and this concept of before it began Meaders.
We've already seen in the previous more D-Will where we learn about our default meters in case of metrics
in Cartland.
So as of now let us remove it.
So here the compiler is not happy because we have to pass some value here.
So here I would simply correct the bug from here.
And pasted inside the primary constructor of God.
Right.
So when this statement will be executed inside the primary constructor this being a variable will get
the value of.
And now the same thing we can apply for the color variable as well.
So here inside the primary constructor and the holder of any well you can simply define that color of
the type of string.
And now here as well you can define their default value.
Let's see black.
Right.
So as of now let us remove it.
And now let us remove this that color from here.
We don't need it.
So here at the bottom you will find some error.
It simply says that we need to pass some value because the primary constructor of the animal contains
under-paid meter.
So here are they we do is I simply pass some value not data value.
I have to get it from this dog constructor.
So for this I have to modify my doc constructor.
Let's modify it.
Let us define that pyramided as colored string Komal that breede.
So now inside the animal I can simply pass color and now add that all I am having some added h simply
expects to be 10 meters.
And now I have one bit I need to define.
So here as a first bit I could simply pass black Come on X and feasted here going up.
And now.
So what is happening here is that when this deal will be executed it will simply call the primary constructor
of this dog plus.
So here is our primary constructor.
So inside this deescalated meter we will get the value of black and this property variable of breed
will get the value of.
And now in return I'm calling the superclass constructor.
That is the Animal class constructor where the value of color is actually this one right which is actually
black.
So this statement will simply pass in the value of black which will fall here.
Add the property variable of this class of animal.
So here inside the class of dog let us do fine our innate block and it has some value.
And now let us copy this block from here and pasted inside the animal as well.
And are just mortified I called animal and here removed the read.
Right.
And now let us run the code and let's see if everything is looking fine.
So here we go in the output console we are getting from animal in it black from dog in it black and
white.
So what is exactly happening is that when the state is executed it simply triggers this method or just
constructor which in turn simply cause the super constructor animal and simply pass it as a value deadfalls
here and has regained the value of the Kalid inside the animal.
Plus and in that scene we hear we get the value of the breed.
Right.
And here we are finding the values.
So in this way when we use constructor's in case of inheritance we have to compulsorily follow this
guideline.
Always call the superclass primary constructor.
If you are having it right and now at the end just to avoid confusion.
Always remember that sequence.
So here the first bit I mean there is actually lot here.
So in there derived class as well the first fit I did always make it color just to avoid any confusion.
Now if you are in multiple constructors in your class such as if you want to use the secondary constructor
instead of primary constructor then let's see.
Var genius.
Do we have to make.
So here let us remove the in it and block and again it doesn't move it from here.
And no I was simply defined my second reconstructed and what I would do is I would simply correct these
meters from here.
And thought X and pasted right here.
And let us remove the declaration here.
And now the second constructor has its own body.
And here as well.
I will create a second constructor and let us go to this statement from here.
And based it inside the animal constructed right and it has its own body.
And now as for the rule the second constructor cannot contain the valid keyword that is you can not
declare the properties inside the secondary constructor.
So what I will do is I will simply copy it and define it as a property inside this that I like this.
And now I would simply remove the very key word from here and inside the constructor I would simply
use this dark color equal to it so as to initialize our color variable from here.
Right.
And now here it shows some error.
The primary constructor call expected.
So here we need to simply use galloon SPEEs this but I give a call to the primary constructor as well.
This one.
And now inside the class dog again we have some error.
So what we do is let us move this color from here.
And now inside this we need to remove this that keyword because we can not have the properties inside
the secondary constructor.
So let us define the VAT breed of that type of string equal to let's say a blank string.
And now inside the constructor I will again use this dot be equal to B.
Right.
And now aspartate it was defined inside the Cartland.
Whenever we are using the secondary constructor we need to call the superclass constructor as well.
So from this constructor we need to call this constructor.
So here what they would do is I would simply use galloon and then call super And as a filmmaker I would
simply pass this color so that as far as this Galit right.
So this super statement will simply call this constructor defined here.
And here it is showing some error.
Let us do one thing and move it from here and also from here.
So this rectifies our code.
So in case of second preconstructed What do we have here is this is our last dog that inherits all the
properties from the animal class.
Right.
And here we have defined a secondary constructor.
So when the statement the top is exec you did this.
This constructor is actually triggered so will get the value of that and we will get the value of.
And this statement in return will simply trigger the superclass constructor that is defined here right
which in turn was simply initialized the color variable.
And again since we have removed the primary constructor declaration from here.
So we don't need to call the primary constructor from here.
So it doesn't remove it.
And this rectifies our error.
And now let us do one thing.
Let us try to put in some values like say inside the constructor and let us copy this statement and
pasted inside this construct as well.
And here I would simply use from animal as so check if we are getting the values or not.
And now let us read the code.
So again in the output we get from animala class Vigurs the output as black and insert dog.
We're getting the value of black and pug respectively.
So our code is now working perfectly fine and each plus is getting a student value with the help of
constrictors in Garling right.
So in this video we learn about how do we use the primary constrictors and also the secondary constrictors
in case of Cartland.