Home > Blockchain >  Unresolved Reference in Kotlin for an Interface defined in Base Class when addressed using Child Cla
Unresolved Reference in Kotlin for an Interface defined in Base Class when addressed using Child Cla

Time:02-11

Hi I am trying to address an interface "CallbackTest" which is defined in my AudioBase.java using AudioChild.java which inherits the AudioBase.java. But it gives compilation error of Unresolved reference when trying to do so.

Here are my class definitions:

AudioBase.java

package com.testapp.kotlinexample.classes;

public class AudioBase {

    public interface CallbackTest {
        void onCall(int var1);
    }

}

Child Class AudioChild.java

package com.testapp.kotlinexample.classes;

import android.util.Log;

public class AudioChild extends AudioBase{

    private static final String TAG = "AudioChild";

    public void someOtherMethod() {
        Log.i(TAG, "in someOtherMethod()");
    }

}

MainActivity.kt

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.testapp.kotlinexample.classes.AudioChild


class MainActivity : AppCompatActivity() {

    private val TAG = "TestApp"
    private var mContext: Context? = null
    private val stateCallback =
        AudioChild.CallbackTest { // Compilation error on this line
            Log.i(TAG, "onCall:() called")
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mContext = applicationContext
    }
}

When I use AudioChild.CallbackTest I get below compilation error:

Unresolved reference: CallbackTest

Can someone please help me understand why this error is coming?

CodePudding user response:

I'm not an expert in kotlin but I know java and googled a bit:

Kotlin: How can I create a "static" inheritable function?

Static methods (and it appears class/interface definitions) are not inherited.

Basically only instance (non-static) members are inherited. Everything statically (class-based) referenced will have to use the class prefix they are defined in.

In this case:

private val stateCallback =
    AudioBase.CallbackTest { // Compilation error on this line
        Log.i(TAG, "onCall:() called")
  • Related