Home > Net >  Using function from parent class inside class with Kotlin
Using function from parent class inside class with Kotlin

Time:12-29

I have the following code in Java which works perfectly well:

public class MainActivity extends AppCompatActivity {
    private final class EchoWebSocketListener extends WebSocketListener {
        @Override
        public void onMessage(WebSocket webSocket, String text) {
            output("Recieving: "  text);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
    }
    private void output(final String txt) {
        //do something on UI
    }

}

Trying the same thing in Kotlin won't work; I have tried as follows but the function is unaccessible from EchoWebSocketListener. Here is the code in Kotlin:

class MainActivity : AppCompatActivity() {
    lateinit var start: Button
    lateinit var output: TextView
    var client = OkHttpClient()

    class EchoWebSocketListener : WebSocketListener() {
        override fun onOpen(websocket: WebSocket, response: Response) {
            websocket.send("Authentication - Samir")
            websocket.send("getScore")
        }
        override fun onMessage(Websocket: WebSocket, text: String) {
            output("Recieving: "   text)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    private fun output(text: String) {

    }
}

How can I make the function output accessible from the overriden methods in EchoWebSocketListener?

CodePudding user response:

Thanks to @Pawel; found the inner key word to use at the class; see documentation. Works perfectly:

class MainActivity : AppCompatActivity() {
    lateinit var start: Button
    lateinit var output: TextView
    var client = OkHttpClient()

    inner class EchoWebSocketListener : WebSocketListener() {
        override fun onOpen(websocket: WebSocket, response: Response) {
            websocket.send("Authentication - Samir")
            websocket.send("getScore")
        }
        override fun onMessage(Websocket: WebSocket, text: String) {
            output("Recieving: "   text)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun output(text: String) {
        //do something
    }
}

CodePudding user response:

You need to private fun output(text: String) this function in the EchoWebSocketListener class you can try like this

 class EchoWebSocketListener : WebSocketListener() {
    override fun onOpen(websocket: WebSocket, response: Response) {
        websocket.send("Authentication - Samir")
        websocket.send("getScore")
    }
    override fun onMessage(Websocket: WebSocket, text: String) {
        output("Recieving: $text")
    }

    private fun output(s: String) {
            TODO("Not yet implemented")
    }
}

Otherwise, you can create a Static method like this in MainActivity

object st{
    @JvmStatic
    fun output(s: String) {
        TODO("Not yet implemented")
    }
}

Both methods are working.

  • Related