0

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?

Dev-0023
  • 3
  • 1
  • It does not import AudioChild.CallbackTest. It shows error. It is only importing AudioBase.CallbackTest correctly. – Dev-0023 Feb 09 '22 at 10:01

1 Answers1

1

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 { 
        Log.i(TAG, "onCall:() called")
Joeblade
  • 1,735
  • 14
  • 22
  • Thanks for the quick reply, yes in this way using the AudioBase class it works. Earlier in Java I used to address the interface using the Child class prefix, so didn't know in Kotlin it does not inherit. – Dev-0023 Feb 09 '22 at 10:04