Reputation:
I have my custom SDK code where I have a manager class which has a couple of functions say "function1(abc,def) " , function2(abc,def) and function3 (abc,def). Now I have an activity in which I am trying to use these functions from my SDK. How do I inherit the functions already used in the managers functions + define my own?
here's my code in manager:
override fun function1(reader: Reader?, openType: OpeningType?) {
some code
}
override fun function2(reader: Reader?, openingResult: OpeningResult?) {
some code
}
override fun function3(reader: Reader?, openType: OpeningType?, openingStatus: OpeningStatus?) {
some code
}
Now I have an activity where I use this SDK as a dependency, and I would like to use these functions such that
class MyActivity : AppCompatActivity(),Managerlistener {
override fun function1(reader: Reader?, openType: OpeningType?) {
// super. some code?
}
override fun function2(reader: Reader?, openingResult: OpeningResult?) {
// super. some code?
}
override fun function3(reader: Reader?, openType: OpeningType?, openingStatus: OpeningStatus?) {
// super. some code?
}
...
}
Now, you may have noticed I hav e super.some code commented out, thats because its the java way of doing things, not sure how to go about inheriting the code in kotlin(super equivalent in kotlin? ) so I can inherit that code and on top of that add my additional code. How do I go about the same? Pretty new to kotlin and not sure about the constructs used.
Upvotes: 0
Views: 2364
Reputation: 317
Let me add a bit on top of iFanie's answer.
What he says is true. In Java as in Kotlin, multiple inheritance is not allowed. A class can only extend 1 class but can implement many interfaces.
Since Activity is a class and not an interface, you are not allowed to extend a second class from your activity.
You have 2 approaches to solve this problem
You would have something like this :
Activity <--- CustomParentActivity {doSomething(), doSomethingElse()} <--- YourActualActivity
Another insteresting thing to look at is the default implementations in interfaces. With Java 8, you can provide a default implementation in an interface that may be enough for your needs.
In Kotlin you have the equivalent if you look at https://kotlinlang.org/docs/reference/interfaces.html
interface MyInterface {
val prop: Int // abstract
val propertyWithImplementation: String
get() = "foo"
fun foo() {
print(prop)
}
}
class Child : MyInterface {
override val prop: Int = 29
}
Upvotes: 0
Reputation: 996
You can't inherit from more than one super classes, so assuming ManagerListener
is an interface, what you have written is correct. As for super calling, it's same in kotlin but in your case you don't need it since you are overriding interface methods without any super
implementations.
Also, just something for you to look at since it does not really apply here, in kotlin you can also use the concept of delegation, which means that you allow an instance of class implementing an interface to give your class the attributes of that interface.
Upvotes: 1