Updated February 14, 2023
Introduction to Kotlin Data Class Inheritance
Kotlin data class inheritance is used to hold the data; this class contains the utility functions and standard functionality driven by data. It has the data for the application; it is like a class pojo which we use in java for holding the data. In java, we are creating a setter and getter method for accessing class properties for the data class.
Key Takeaways
- The class may be extended into other classes for implementing inheritance; if suppose we are using the kotlin version before 1.1, then the class only implements the interface.
- When declaring the data class, the compiler automatically generates the function.
Overview of Kotlin Data Class Inheritance
In kotlin, when we declare the data class, the compiler will automatically create the supporting methods required for accessing the member variable for the class. The compiler will generate the setter and getter methods for the constructor’s parameter. The class, which was considered the data class, will fulfill the conditions of the primary constructor, which contains at least only one parameter. We need to consider all primary constructor parameters as var of Val. It is beneficial and essential in kotlin for declaring the class.
How to Use Extend Kotlin Data Class Inheritance?
We need to fulfill the following conditions as follows:
- We need to declare at least one parameter to the primary constructor.
- Our data classes cannot open sealed abstracts or inner.
We cannot extend the class but suppose we want to implement the same feature; then, we can declare the superclass to override the properties into the sub-class. The data class in a kotlin is holding the data object. For using, we need to use the below steps. For using data class inheritance first, we are creating the new project in Intellij idea by using kotlin language as follows.
1. For extending kotlin data class inheritance, in the first step, we create the new project in kotlin name as kotlin_Class by using the Intellij idea. At the time of creating the project, we provide the parameter below.
Name – kotlin_Class location – \Documents
Language – Kotlin Build system – Intellij
Jdk – Java version 11 Project – New project
2. Data classes in kotlin are replacements for pojo in java. Hence it will allow inheritance. The below example shows the use of the abstract class for extending the data class in a kotlin. The abstract class in kotlin is used to declare all parameters as abstract into the parent and then override them into the child class.
Code:
fun main(args: Array<String>) {
abstract class student {
abstract var roll_no: Int
abstract var stud_name: String
}
data class stud (
override var stud_no: Int = 101,
override var stud_name: String = "ABC",
var address: String
) : student()
}
Output:
3. The below example shows the inheritance used for extending the data class into the kotlin. We are extending the data class in kotlin by using inheritance as follows.
Code:
interface kotlin {
val name:String
}
interface Derived_kotlin : Base_kotlin {
val derived_name:String
}
fun main(args: Array<String>) {
data class P(override val name:String) : Base_kotlin
data class Q(override val derived_name:String,
private val b:Base_kotlin) : Derived_kotlin, Base_kotlin by b
val stud1 = P("ABC ")
val stud2 = Q("PQR!", stud1)
print(stud2.name)
print(stud2.derived_name)
}
Output:
4. We can also use open classes for extending the data class in a kotlin. Extending the data class into kotlin is to declare all the properties of the parent class. We are using the override method to override the properties of a sub-class for extending the data class. The below example shows the use of an open class for extending the data class in kotlin as follows.
Code:
fun main(args: Array<String>) {
open class kotlin {
var stud1 = false
var stud: String? = null
}
data class student2 (
var stud3: Long
) : student1 ()
}
Output:
Examples
Different examples are mentioned below:
We are using the primary function for declaring the properties of a variable into the data class.
Example #1
In the below example, we are declaring the class name as kotlin. When declaring the class, we are declaring two vales with val methods. Also, we are using the primary method in the below example.
Code:
data class kotlin (val stud_name: String, val stud_age: Int)
fun main (args: Array<String>) {
val stud = kotlin ("ABC", 10)
println ("Stud Name = ${stud.stud_name}")
println("stud_age = ${stud.stud_age}")
}
Output:
Example #2
In the example below, we are creating the two data classes and the abstract class.
Code:
data class Student (val stud_name: String, val stud_age: Int)
fun main(args: Array<String>) {
val stud = Student("ABC", 10)
println ("Stud name: ${stud.stud_name}")
println("Stud Age: ${stud.stud_age}")
val stud1 = school (1L,"India","321")
println(stud1.stud_add)
}
abstract class Res {
abstract var stud_id: Long
abstract var stud_add: String
}
data class school (
override var stud_id: Long = 0,
override var stud_add: String = "",
var add: String
) : Res()
Output:
FAQ
Given below is the FAQ:
Q1. Why is kotlin not supporting the inheritance of data class?
Answer:
We cannot extend a data class or make the same abstract. So we cannot use the same in a domain model, which was core.
Q2. Why is composition not appropriate in it?
Answer:
We have final classes in a JVM, which is faster and very easy to optimize; Kotlin will reuse record ideas for java byte code. Disabled data class inheritance covers most cases and makes code easy.
Q3. How to declare the properties into the class body?
Answer:
The compiler only uses the properties defined by the primary constructor for the automatically generated function.
Code:
data class stud (val stud_name: string) {
var stud_age: Int = 10
}
Conclusion
The class, which was considered the data class, will fulfill the conditions of the primary constructor, which contains at least only one parameter. It holds the data; this class contains the utility functions and standard functionality driven by data.
Recommended Articles
This is a guide to Kotlin Data Class Inheritance. Here we discuss the Overview, Examples, and how to use extend kotlin data class inheritance with FAQ. You may also have a look at the following articles to learn more –