0

I'm looking to create an object structure that has a dynamic class that I would want to change:

Top level:

data class TopLevel(
  var myCustomClass1: MyCustomClass1,
  var myCustomClass2: MyCustomClass2)

Second level:

data class MyCustomClass2(var dynamicClass: DynamicClass)

The DynamicClass is the the one I want to change.e.g I may want to use DynamicClass or DynamicClass2 within MyCustomClass2.

What is the best way to do this using kotlin?

2
  • what is your goal with this? Commented Jul 5, 2018 at 20:46
  • Within MyCustomClass2 I want to be able to change which object is passed into it. Commented Jul 5, 2018 at 20:51

1 Answer 1

2

Since Kotlin is a statically typed language, you'll need to find some way to describe your abstract class, e.g. by using an interface:

interface IDynamicClass

class DynamicClass : IDynamicClass
class DynamicClass2 : IDynamicClass

data class MyCustomClass2(var dynamicClass: IDynamicClass)

This allows you to instantiate the MyCustomClass2 with any instance implementing the IDynamicClass interface:

MyCustomClass2(DynamicClass())
MyCustomClass2(DynamicClass2()) 
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.