# Kotlin class inherit java class. But kotlin compiler say type mismatch

**URL:** <https://discuss.kotlinlang.org/t/kotlin-class-inherit-java-class-but-kotlin-compiler-say-type-mismatch/23934>\
**Category:** Support\
**Created:** [January 16, 2022, 8:09am UTC](https://discuss.kotlinlang.org/t/kotlin-class-inherit-java-class-but-kotlin-compiler-say-type-mismatch/23934 "2022-01-16T08:09:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlarudfo93](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@rlarudfo93](https://discuss.kotlinlang.org/u/rlarudfo93)\
**Post date:** [January 16, 2022, 8:09am UTC](https://discuss.kotlinlang.org/t/kotlin-class-inherit-java-class-but-kotlin-compiler-say-type-mismatch/23934/1 "2022-01-16T08:09:10Z")

</div>

Hi, I am clone coding [The Definitive Guide to Spring Batch](https://github.com/Apress/def-guide-spring-batch) using kotlin. But I faced a problem about covariant. When I build step by StepBuilder, kotlin compiler say `Type mismatch. Required: ItemReader<out Customer!> Found: CustomerFileReader`.

```nohighlight
@Bean
@StepScope
fun customerItemReader(
    @Value("#{jobParameters['customerFile']}") inputFile: Resource?
): FlatFileItemReader<Any> {
    return FlatFileItemReaderBuilder<Any>()
        .name("customerItemReader")
        .lineMapper(lineTokenizer())
        .resource(inputFile!!)
        .build()
}

@Bean
fun customerFileReader(): CustomerFileReader {
    return CustomerFileReader(customerItemReader(null))
}

@Bean
fun copyFileStep(): Step {
    return stepBuilderFactory["copyFileStep"]
        .chunk<Customer, Customer>(10) // I don't want change this to chunk<Customer, Customer>(10)
        .reader(customerFileReader()) // This line has problem about covariant
        .writer(itemWriter())
        .build()
}

```

```nohighlight
class CustomerFileReader(
    private val delegate: ItemStreamReader<Any>
) : ItemStreamReader<Customer> {

    override fun read(): Customer? {
        if (curItem == null)
            curItem = delegate.read()

        var item = curItem as Customer?
        curItem = null

        if (item != null) {
            item.transactions = mutableListOf()

            while (peek() is Transaction) {
                item.transactions.add(curItem as Transaction)
                curItem = null
            }
        }
        return item
    }

```

I thought that `CustomerFileReader` is subtype of `ItemReader<out Customer!>`. Let me know why compilation error occur and how to solve this problem.  
Thank you!

[Github link](https://github.com/kyungrae/study/blob/faef5209975920f7fe9dd6e3c50aaa025069a21d/def-guide-spring-batch/chapter07/src/main/kotlin/me/hama/file/multiline/MultiLineJob.kt#L98-L105)

---

<div class="post-metadata">

**Author:** ![rlarudfo93](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@rlarudfo93](https://discuss.kotlinlang.org/u/rlarudfo93)\
**Post date:** [January 17, 2022, 8:52am UTC](https://discuss.kotlinlang.org/t/kotlin-class-inherit-java-class-but-kotlin-compiler-say-type-mismatch/23934/2 "2022-01-17T08:52:05Z")

</div>

Sorry, It’s not problem about covariant. I just import Customer class in different package. 😂
