Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call 'super()' from a secondary constructor in kotlin?

I have just started using kotlin and I have a block of code in java which I have to convert to kotlin. This is the java code:

public class NonSwipeableViewPager extends ViewPager
{
    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    private void setMyScroller() {
        //some code
    }
}

If there was only one constructor in this code, I could have written like this:

class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }

But, as there are two constructors and each constructor calls super() method, I couldn't figure out how can I convert this code to kotlin. The closest I have achieved is this:

class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }

But, in this code, i am getting the following error in this line super(context!!, attrs): primary constructor call expected So, how can I call super() from the secondary constructor?

like image 271
Jayesh Babu Avatar asked Jan 20 '20 05:01

Jayesh Babu


People also ask

How do you call a super class constructor in Kotlin?

If the subclass does not contain primary constructor, then we have to call the superclass constructor (primary or secondary) from the secondary constructor of subclass using the super keyword. We also need to initialize the superclass secondary constructor using the parameters of subclass.

How do you call a secondary constructor in Kotlin?

To do so you need to declare a secondary constructor using the constructor keyword. If you want to use some property inside the secondary constructor, then declare the property inside the class and use it in the secondary constructor. By doing so, the declared variable will not be accessed inside the init() block.

How do I call the Kotlin super class method?

If derived class does not contain any primary constructor then it is required to call the base class secondary constructor from derived class using super keyword. For example, open class Patent { constructor(name: String, id: Int) {


2 Answers

class NonSwipeableViewPager: ViewPager {

    init {
        setMyScroller()
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    private fun setMyScroller() {
        //some code
    }
}

Notice that neither secondary constructor requires the call to setMyScroller(), since the init block will be called immediately after the super call.

like image 65
Ryan Avatar answered Oct 18 '22 23:10

Ryan


Change your code like this

class NonSwipeableViewPager : ViewPager {
    constructor(context: Context?) : super(context!!) {
        setMyScroller()
    }

    constructor(context: Context?, attrs: AttributeSet?) : this(context) {
        setMyScroller()
    }

    private fun setMyScroller() { //some code
    }
}

instead of

 class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }
like image 1
Arunachalam k Avatar answered Oct 19 '22 00:10

Arunachalam k