Detailed Guide About Constructors in Kotlin Programming Language 2020

Today we will see the concepts of constructor which are very very important for any programming language and it is important for certain language programmers as well so in code we have that constructor as a primary constructor and if you want to make some more constructor there will be a concept of secondary constructor but it is directly or indirectly that as shared with the primary constructor here in this example that we have seen that function main. 


Detailed Guide About Constructors in Kotlin Programming Language 2020


We have this area as a string which is an argument and we are taking this person so the person is a class name this is that we have two uh these variables over here first name and this age integer of type and it's a string. 


Detailed Guide About Constructors in Kotlin Programming Language 2020


So we have to just assign the value over here this uh under this so first name is CSS friends and age is 15 so print these two values, uh using this person one and dot first name and person one dot a so we have to create an instance of that uh person class and just invoke uh that one and access these variables which are first name and this is a so once we run this program. 


We will have those outcomes as CSS friends and uh 15. so as we all know that uh ctrl shift f10 is one of the keys that we use for running these programs and as well as to change the font of that is also we have ctrl shift s to make that setting available for font uh variations in size and the type of font we want to use on the editor so here uh the constructor is important and uh usually at the time of and we say that creation of an instance of a class constructor having a wonderful significance of using in a most of these object-oriented programming languages. 


Next thing is uh once we say about the constructor kotlin has as I said you as a type of constructor which is a primary constructor and the other one is the secondary constructor so we use constructor as a keyword to make a secondary constructor uh one uh kotlin class can have one uh primary constructor and one or more secondary constructor. 


So java as java constructor is one of that initializes the member variables and in kotlin the primary constructor initializes that the class whereas the secondary constructor helps to do the same so including us along with including some of the additional logic to the program so this is important now the primary constructor uh that can be declared as a header at the time of the class header. 


Kotlin Programming Language 2020


We use as shown uh in the example so class person here we have a bell and first name that type of string and this is this h which is having so integer so this is uh one of the, uh we can say uh these are the values so now we just try to run this program so run example underscore seven dot kt which is an extension for Scotland now here uh we see that uh this is the result that css trends and age 15 so this is one of the ways of uh creating that primary constructor of a class now uh when we consider the next one is uh having this class human. 


Kotlin Programming Language 2020



We have a concept of secondary constructors so this one is uh the way that we can use that constructor using the constructor as uh this name and age as a very well along with the masses so we have to just bring these uh using this concatenation operator and uh we just making this instance of this human so we have this uh first name is CSS friends and uh then we have age 25 and finally, this is given in this. 


Welcome to the example of secondary constructor and your age is human.h so with the help of this instance it will uh print that age which is fit 25 so when you will run this program you will get this CSS trends and this message along with your age is 25 so high up is one of that masses is going to concatenate so here hi and then the first name css friends and then welcome to the example of secondary constructor your age is hyphen 25 that will be the outcome and to make that secondary constructor.

You have to use this constructor and along with this number of variables you are passing through this so these are the arguments and this name comma h and finally enclose these presses so just run this program and see the outcome example dot this control shift f10 let us see the outcome so we have this outcome yes hi CSS friends to the example of the second reconstructor your age is hyphen 25. 


So this is the way that uh we just use a constructor in kotlin thank you very much being with me in the session if you like these videos please subscribe my channel and I just humble request you that please watch the previous videos as well and try uh to just go through practicing all these three here in these uh Cortland programming. 


Conclusion


I'm using IntelliJ uh their check brains ide which is very very important uh and it's a new set of experience for everyone who is working in the field of enlightenment man so thank you very much.

Post a Comment

Previous Post Next Post