Kotlin Constructor - Primary, Secondary Constructor and Init Block
Вставка
- Опубліковано 5 гру 2020
- After learning about visibility modifiers, we will move to Kotlin Constructor. A constructor is responsible for constructing the object. In kotlin we have primary constructor, secondary constructor and init block.
Constructor is often used to initialize class members. Every kotlin class has a default constructor available if no constructor is defined.
In this video we will learn about Kotlin Constructor. We will learn how to define and use primary constructor. And as inside primary constructor we cannot write any logic, and to write logic we have init blocks in kotlin, and we will learn about init blocks as well. And finally we will also learn about secondary constructors.
Basically a Kotlin class can have only single primary constructor and as many init blocks and secondary constructors as required.
If you are having difficulties navigating from one lesson to another, then click on this link and it will open the complete playlist of this Kotlin Object Oriented Programming. In this playlist all lessons are already arranged in chronological order.
👉 bit.ly/3lMLtpj
All the codes about this Kotlin Android Tutorial for Beginners are organized sequentially in this repository
👉 bit.ly/2UoyiQf
And if you need more support, then you can join the Simplified Coding’s Quora Space to post your questions.
👉 www.quora.com/q/simplifiedcoding
If you want to get in touch with me; you can find my contact details here:
👉 www.simplifiedcoding.net/about/
Finally, If you love my content please SHARE my courses to your friends. You can share this course to your Facebook, WhatsApp etc.
For more contents related to Software Development visit my website.
👉 www.simplifiedcoding.net
You can SUPPORT me if you want
👉 Patreon: / belalkhan
👉 UPI (India Only): probelalkhan@okaxis
👉 PayTM (India Only): +917549161079
👉 PayPal: www.paypal.me/probelalkhan
You can also check some of my other courses from these links
👉 Android MVVM Tutorial: bit.ly/3iLfVzu
👉 Android Game Development Tutorial: bit.ly/38JmltC
👉 Retrofit Android Tutorial: bit.ly/2DEwAl1
👉 Android RecyclerView Tutorial: bit.ly/2TqVjPz
👉 Android Room Tutorial: bit.ly/2PfQHeN
👉 Firebase Auth Tutorial: bit.ly/2WUGhEo
👉 Android Notification Tutorial: bit.ly/2Sc6jQl
👉 Building a Complete Wallpaper App: bit.ly/30UN8zV
👉 Build Tic Tac Toe in Android: bit.ly/30goxW0
❤️❤️ Thank You All for the Love and Support ❤️❤️
#KotlinOOP
#OOPsConcept
#LearnKotlin - Наука та технологія
you sir are one of the BEST youtube tutors i have ever seen, you should have tens of thousands of subscribers- best description of primary and secondary constructors- seriously!! keep up the good work
Thank you very much! Nice explanation!
Thanks loved it!
Thank you very much for this video. Coming from Java I was confussed about constructors in Kotlin.
Great video thanks bro keep on
Thank you so much. After i finish this oop i will build my android!
Hi
While creating instance of class, Secondary constructor's parameters value is assign first then init block is executed then secondary constructor's body execute?
Why like this?
If the parameters value is assigned first then body's println should print but it's print after init block
init block is called after primary constructor.
And if you are making a call to secondary constructor then, in this case secondary constructor will call primary constructor and then init block.
But when you call secondary constructor, primary constructor is called in the first line only and hence init block is also called and then the secondary constructor body is executed.
Hope you got it.
HI Belal. Thanks for sharing this knowledge. My doubt is, as you mentioned, we cannot have any logic written inside the primary constructor. If we need to write then it should be within init block but there are few things like calculating the volume which is done inside the body of the primary constructor directly rather than writing in init block. How is it possible??
Probably are you referring to the volume property, that's is possible because you can write code inside the getter or the setter, but that code is called only when the getter or the setter are called. With the initializer blocks you have the code - generally is used for multiple lines of code - executed when the objects are created using the class.
oooh so the constructor with body is like init block
wow thanks for this amazing knowledge
btw how to use secondary constructor for app development in android phone
1st 🥰Will Surelly watch These kotlin Toutorial 🙂 Thanks for the Toutorial Please Make a Video on Firebase Pushed Notifications #request
what is the use of multiple init block
Haha good question, I have not came across any practical scenario where I had to create multiple init blocks.
And it is recommended to have only one init block of required.
So I can say there is no use of it.
👏🏻👏🏻👏🏻
Could you make video about kotlin multiplatform?
Yeah I have plans about it
what is use/purpose of "lateinit" in Kotlin?
Purpose of what?
@@SimplifiedCoding sorry, type error..its what is use/purpose of "lateinit" in Kotlin?
For this scenario
#1 you cannot assign a value to the variable while declaring it
#2 as you cannot assign a value, you can use null as in kotlin you have to give a value while declaring a variable
#3 If you give null its ok, but the problem is from now onwards everywhere you need to use null safety operator with that variable
#4 you can use lateinit when you dont want to use null safety
But it may case a null pointer exception because lateinit means you dont have the value now but you will give it later. And you do not need to use null safety with lateinit.
But use it only when you are absolutely sure that it will not be null.
despite of the many implicit functionality of creating objects under the hood sometimes we are confused whether it is implicit or explicit
Thanks for the video!Your one observation is wrong in secondary constructor example. You said "First init block is called,then secondary constructor is called and then secondary constructor will call primary constructor".Wrong.The init block is always called after the primary constructor. So first primary constructor is executed.Then immediately init block starts to execute.After init block , secondary constructor block starts to execute.
I think I said exactly what you are saying. Anyways I will watch the video. May be I said wrongly. 👍
thank you
So the order is
Primary constructor
Init block
Init block
Secondary constructor
No init block is called after primary constructor.
And if you are making a call to secondary constructor then, in this case secondary constructor will call primary constructor and then init block.
In any case primary constructor has to be called. And if you have multiple constructor then only the calling constructor will be called and then primary constructor and then init block.
But when you call secondary constructor, primary constructor is called in the first line only and hence init block is also called and then the secondary constructor body is executed.
Hope you got it.
@@SimplifiedCoding yeah.thank you.
You should consider to become a teacher!
I didn't understand anything at all
Can you please be a little specific about what exactly you did not understand?
true
mabey u dont understand because you have put no effort into learning this language..This man has perfectly described the concepts of primary and secondary constructors in Kotlin
why indian people always have to use "sir" phrase in their comments? and why are they so formal? its just youtube