Hi this is sharings and hard and welcome to the next video of this object oriented programming in Cartland.
Now in this video shake out how do we define a class in case of Codling similar to Java in case of Cartland.
We have the class definition like this class keyword followed by the class name.
And this is the class Bardy.
And now this class name customer is actually known as the holder of the class.
So always remember this.
Now how do we define the constructor in case of Cartland.
So here is some example class customer followed by Bechard open and close inside which we can simply
define some parameters such as name string like this.
So this name is actually the string and it is actually the primary constructor.
So this is the way we can define the primary constructor in case of Codlin.
Now if you notice here this constructor does not contain any Borghi unlike in Java where we had the
constructor defined explicitly inside the class body.
Now the question arises how can we initialize all the field variables in case of Cartland.
So for this we have entered block.
So in it followed by Curly Blackard open and close inside this we can simply initialize all the properties
all the field variables.
In case of Cartland.
So this is a typical example of a class containing the primary constructor and also in a block.
Let us not proceed and check out what are the concepts and are we really covered in this value.
So first of all we really check out what are properties in case of Cartland that are also known as the
Field Theory was in case of Java.
And then given that check out the significance of the primary constructor in a block primary constructor
with the property and also the primary constructor it just goes.
Now what is the basic difference between these two such as them reconstructed with property and with
it goes.
We will check out this video.
So let's go to Stockard.
So here inside the ID here I have my main medtech now on the side.
Let us create our class such as a student right.
And this is a class Bardy.
And I guess in my previous videos I already taught you how do we define a class in case of Cartland.
Now to instantiate this class we can simply use.
Let's say that space student equals to student.
So typically in case of Cartland we define we instantiate our class by using that espies student equal
to student.
Now notice here we don't have any new keyword unlike in Java.
Right.
So the syntax is more cleaner and more precise compared to Java.
And now suppose student has some name so far that inside the Student class as a part of Bardy we can
simply define some field variables which in Cartland is known as property.
So here I will simply use that name which is of their type of string.
Now here it will show some error that the var name must be initialized or should be declared as abstract.
So here I will simply give it a default name as let's say dummy let's call it our domain name.
And now here I will simply initialize this property inside the main method by simply using object names
student Dorte let's say name equal to and which is my name really cool.
And now if I want to print the name inside the main method I will simply write up re-instatement name
as let's see Dollard in the pollution level that could open and close student Dollard name.
So if I turn the court right now I will get and in the output with this statement but I.
So here I get student has got a name as sharing.
So here you can notice I have simply created the object of the Student class and and define the property
or the field variable as name equal to dummy which is their default value.
And here I have simply overridden the name of the student as Cherian and here as simply put in good
order statement.
Now now suppose if I want to print this statement inside the class.
Student.
So what do I do.
So here I will simply create in that block.
Right now this block is immediately executed after the creation of the instance of the Student class.
So when the statement is executed this in block will be called right after.
So inside this I would simply code this statement from here and paste it here.
Right now here if you notice the student is not defined inside the class.
Student Right.
Because the school of student is actually applicable inside the main method here.
Right.
So here we do I would simply remove it.
And now here I want to print this name.
So far that what I really do is this student name has been defined here.
Now what if if I pass this value as a metre inside the constructor like we do in case of Java.
So in case of Codlin we have the concept of primary constructor.
So let's say constructor and simply here declared name call on string.
So here I will simply pass this shrink as a bit I get it.
And now there does remove it.
And also I guess I can command this name from here.
Right.
So inside the block I can simply put in the name as Dollard name.
Which is basically this incoming name from this constructor.
Right.
And now here.
As for the Cartland coring standard.
If you do not specify any more fires such as are they inject or something like that.
So here you can simply skip this constructor keyword and appended to the header so our code is now looking
more cleaner.
So this is actually the header along with the primary constructor defined here.
So as soon as this statement will be executed we will get student has got a name of 3 in the output
console.
So they're just out in the cold.
So again in the output we got student has gotten him a string.
Right.
And now suppose I want to print the name of the student offered this statement that is inserted the
main method somewhere here.
Right.
So far they really do is I would simply use student dord name but here if you notice this name is actually
undefined.
But you must be thinking we have simply specified this name as a meter here.
So why aren't we access it here right inside the main method.
This is simply because this name string is actually just acting as a paid meter.
This is not the property of the class student that we had earlier in this class.
Right.
So similar to any other constructor as for example in case of Java you can simply define name here and
now inside init method which is actually the part of the constructor you can see the primary constructor
this name can be initialized as name equal to name.
And now this name is actually this name.
So we have to tell the compiler by using this keyword.
So this name which is basically this name has been now initialized to this name in-coming.
Right.
Which is actually string.
So finally this.
That name is now has now got the value of sharing.
So inside the main method and now we can simply access student dork name and can simply print the value.
But it is simple.
So if you copy this statement and suppose paste it here.
And now he had to simply put it into Felician student Dorte name.
Then this could simply appear in the value of St and from the main method itself.
And now just to overwrite some confusion let us the name this name as underscored and here as well you
can rename it to underscore and here if you want.
You can also to underscore here that will simply refer to this name variable.
Or you can simply use Dolor name that is actually pointing to this name property variable.
And now ask what our current example we are actually assigning the value to this name variable in two
steps.
That is we are simply passing the name string which comes here.
And then inside the end block we are simply assigning a value to this name.
Now what if if I simply tell you that you can simply declare this variable name property right inside
the primary constructor.
So what I'm talking about is you can simply call a valid space name.
Right.
So here they're discovering this.
And here if you notice we have simply declared a property inside the primary constructor itself.
So instead of doing it in their step we have just done it in one step by simply using the Varrick keyword
inside the primary constructor.
Or if you want the immutable variable then simply use Val here like this.
So the primary constructor simply supports Val and also that as well.
So this can be removed from here and now our code will now again work perfectly fine.
Let us read the code.
So again we got student has got a name as string.
And here let us remove this comment.
This will make our code look more cleaner.
So this was all about this video where we learned how to declare a class the primary constructor and
also we learned about the in it block and along with this we also learned about the property of the
class and also we can declare the property right here inside the class Vardi.
So this was all about this video.
Thanks for watching and have a good day.