As I'm working in the middle of some kotlin project I got a confusion like whether a child class implements another parent class or else implementing an interface? like I am using some interfaces and classes from a jar which I am not aware a lot about it could someone explain to me a way to solve this since I am new to kotlin.
For Example:
a class definition
abstract class Employee (val firstName: String, val lastName: String) {
abstract fun earnings(): Double
}
which is extended by some other class
abstract class Employee (val firstName: String, val lastName: String) {
// ...
fun fullName(): String {
return lastName + " " + firstName;
}
}
Similarly an Interface
class Result
class Student
interface StudentRepository {
fun getById(id: Long): Student
fun getResultsById(id: Long): List<Result>
}
Interface implementation
class StudentLocalDataSource : StudentRepository {
override fun getResults(id: Long): List<Result> {
// do implementation
}
override fun getById(id: Long): Student {
// do implementation
}
}
In Kotlin, to inherit from a class you have to write its primary constructor, so you will always see the parent class followed by ()
, sometimes with things inside.
Interfaces don't need this. You just write the names of the interfaces and that's it.
So if you see brackets after a name, that's a parent class. Otherwise its an interface.