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?
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.
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.
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) {
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With