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
  • Наука та технологія

КОМЕНТАРІ • 35

  • @rxtechandtrading
    @rxtechandtrading Рік тому +1

    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

  • @Matematica_Aplicada
    @Matematica_Aplicada 2 роки тому

    Thank you very much! Nice explanation!

  • @Ari-118
    @Ari-118 3 роки тому +1

    Thanks loved it!

  • @SB-ox1co
    @SB-ox1co 9 місяців тому

    Thank you very much for this video. Coming from Java I was confussed about constructors in Kotlin.

  • @zakariarada1856
    @zakariarada1856 3 роки тому

    Great video thanks bro keep on

  • @kakyoin4535
    @kakyoin4535 5 місяців тому

    Thank you so much. After i finish this oop i will build my android!

  • @kishorramani007
    @kishorramani007 3 роки тому

    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

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому +1

      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.

  • @keshav661
    @keshav661 2 роки тому +1

    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??

    • @soma7891
      @soma7891 2 роки тому

      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.

  • @yotsusan_machi
    @yotsusan_machi 3 роки тому

    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

  • @muskanyousaf20
    @muskanyousaf20 3 роки тому

    1st 🥰Will Surelly watch These kotlin Toutorial 🙂 Thanks for the Toutorial Please Make a Video on Firebase Pushed Notifications #request

  • @olahuuber5540
    @olahuuber5540 3 роки тому +1

    what is the use of multiple init block

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому +2

      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.

  • @jishnupdileep3143
    @jishnupdileep3143 Рік тому

    👏🏻👏🏻👏🏻

  • @kubux6426
    @kubux6426 3 роки тому +2

    Could you make video about kotlin multiplatform?

  • @anil801
    @anil801 3 роки тому

    what is use/purpose of "lateinit" in Kotlin?

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому

      Purpose of what?

    • @anil801
      @anil801 3 роки тому

      @@SimplifiedCoding sorry, type error..its what is use/purpose of "lateinit" in Kotlin?

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому +1

      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.

  • @footballCartoon91
    @footballCartoon91 2 роки тому

    despite of the many implicit functionality of creating objects under the hood sometimes we are confused whether it is implicit or explicit

  • @bhuvneshvarma7843
    @bhuvneshvarma7843 3 роки тому +1

    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.

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому

      I think I said exactly what you are saying. Anyways I will watch the video. May be I said wrongly. 👍

  • @bilelrahmouni01
    @bilelrahmouni01 9 місяців тому

    thank you

  • @vengateshm2122
    @vengateshm2122 3 роки тому

    So the order is
    Primary constructor
    Init block
    Init block
    Secondary constructor

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому +1

      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.

    • @vengateshm2122
      @vengateshm2122 3 роки тому

      @@SimplifiedCoding yeah.thank you.

  • @denkling
    @denkling 2 роки тому

    You should consider to become a teacher!

  • @ipvoodoo
    @ipvoodoo 3 роки тому +1

    I didn't understand anything at all

    • @SimplifiedCoding
      @SimplifiedCoding  3 роки тому +1

      Can you please be a little specific about what exactly you did not understand?

    • @richenjoshi7054
      @richenjoshi7054 2 роки тому

      true

    • @rxtechandtrading
      @rxtechandtrading Рік тому +1

      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

  • @bezelyesevenordek
    @bezelyesevenordek Рік тому

    why indian people always have to use "sir" phrase in their comments? and why are they so formal? its just youtube