1

I have these Java classes in a .java file:

public class BaseOuter
{
    public class BaseInner
    {
    }
}

And I have these Kotlin classes in a .kt file:

class DerivedOuter : BaseOuter()
{
    class DerivedInner : BaseOuter.BaseInner()
    {
    }
}

This Kotlin code in Android Studio gives me an "Unresolved reference: BaseInner" error. So I can inherit from the BaseOuter class, but deriving from Java's BaseOuter.BaseInner is not syntactically correct (but if I try the same with inheritance from Kotlin classes I get no errors).

Somehow I can't google a same example or the question.

3
  • 1
    For one thing, this doesn't work, because you have an inner class in Java but a static member class in Kotlin. For a class to be inner in Kotlin, you need to mark it with the inner keyword: inner class DerivedInner ... Commented Nov 14, 2018 at 23:34
  • @ErwinBolwidt thanks Commented Nov 14, 2018 at 23:42
  • You're welcome. I didn't think it would solve it (because the error says it can't resolve the reference which doesn't sound like the problem I mentioned) and I didn't have time to try it. Hence only a comment Commented Nov 15, 2018 at 0:15

1 Answer 1

2

The equivalent of Java's non-static nested class in Kotlin is inner class.

To make your code work, you need to use inner keyword:

class DerivedOuter() : BaseOuter() {
    inner class DerivedInner : BaseOuter.BaseInner() {
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.