# Interoperability between Kotlin and Java

**URL:** <https://discuss.kotlinlang.org/t/interoperability-between-kotlin-and-java/7255>\
**Category:** Android\
**Created:** [March 30, 2018, 10:30am UTC](https://discuss.kotlinlang.org/t/interoperability-between-kotlin-and-java/7255 "2018-03-30T10:30:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravik6719](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@ravik6719](https://discuss.kotlinlang.org/u/ravik6719)\
**Post date:** [March 30, 2018, 10:30am UTC](https://discuss.kotlinlang.org/t/interoperability-between-kotlin-and-java/7255/1 "2018-03-30T10:30:51Z")

</div>

Hi All,

I have developed an android app which written in Java. Now I am going to add a new feature to it. I want to implement it in Kotlin as Kotlin supports the interoperability with java.  
I facing some problems  
Eg: 1) I need to load/replace Fragment which is coded in Kotlin from the Activity which is coded in Java.  
2) Passing data to Kotlin fragment/Activity from Java Fragment/Activity  
3) Converting/Casting Objects from Kotlin to Java and Java to Kotlin.

Thank You,  
Ravi.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 30, 2018, 12:43pm UTC](https://discuss.kotlinlang.org/t/interoperability-between-kotlin-and-java/7255/2 "2018-03-30T12:43:10Z")

</div>

Kotlin objects are java objects. Basically they are fully interoperable and interchangeable. If the Java activity wants to use a fragment written in Kotlin it uses exactly the same code as if it were written in Java. The same way with passing data (you set it as extra on the intent that starts the activity / or the fragment parameters) - nothing different. On converting/casting you don’t need to do so, both languages produce class files that are compatible with eachother and all get transformed into dex later.

---

<div class="post-metadata">

**Author:** ![ashiqsayyad](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@ashiqsayyad](https://discuss.kotlinlang.org/u/ashiqsayyad)\
**Post date:** [November 13, 2018, 1:54pm UTC](https://discuss.kotlinlang.org/t/interoperability-between-kotlin-and-java/7255/3 "2018-11-13T13:54:47Z")

</div>

I am trying to make some changes in google sample project

> **[GitHub - googlearchive/android-BasicAndroidKeyStore: This sample has been...](https://github.com/googlearchive/android-BasicAndroidKeyStore)**
>
> This sample has been deprecated/archived. Check this repo for related samples: - GitHub - googlearchive/android-BasicAndroidKeyStore: This sample has been deprecated/archived. Check this repo for r...

I have rewritten written one fragment BasicAndroidKeyStoreFragment in kotlin ( the fragment was originally written in java)

Now in MainActivity.java, I have following piece of code originally to add BasicAndroidKeyStoreFragment

BasicAndroidKeyStoreFragment fragment = new BasicAndroidKeyStoreFragment();  
transaction.add(fragment, FRAGTAG);  
transaction.commit();

Now I crated a replica fragment in kotlin AndroidKeyStoreFragmentKotlin.kt  
Just wanted to know how to add above kotlin fragment in MainActivity.java.
