Updated April 1, 2023
Introduction to Kotlin Abstract Class
The kotlin abstract class is one of the keywords, and it is used to declare and create the class. It cannot be instantiated also unable to create the object for the class, variables, methods, and other default functions attributes also used the abstract keyword. It does not need to annotate using the open keyword; if the function is abstract, we cannot have the body. It must be implemented using the other derived class, which is extended using the abstract class name. It contains both abstract and non-abstract attributes, variables and other functions.
Syntax of Kotlin Abstract Class
The kotlin language has many default keywords, variables and functions for implementing the mobile-based applications. For example, an abstract is one of the keywords for to utilising the class, methods and variables. However, it cannot create the object for to utilising the abstract class feature.
abstract class className(arguments)
{
abstract var variablenName: Data type
abstract fun method()
fun method1()
{
---some coding logics----
}
}
class classname(argument values):className()
{
---some kotlin coding logics—
}
fun main(args:Array<String>)
{
val vars = classname(argument values)
vars.methods() // which is created on the above class which is not declared abstract keyword
}
The above codes are the basic syntax for utilising the abstract keyword in the kotlin language.
How does Abstract Class work in Kotlin?
The abstract keyword is one of the keywords that can be declared using the abstract classes, methods and variables. When we use abstract keywords in the class, it cannot be instantiated so that we cannot create the objects when we declared the class as abstract. However, we can inherit the subclasses from them so that the parent-child class relationship is applicable for these types of classes. Therefore, when we create the abstract class, we are unable to create the object for those classes.
It contains both abstract and non-abstract methods; we also override the methods in both the parent and subclasses members with the help of the @override annotation keyword. And also, the class does not contain anybody, so it does not have any implementation like variable declaration, method creation. The abstract classes are more similar to the interface; however, we cannot store the states, whereas abstract classes can have done it. But in an interface may have the property, but it needs to be the abstract, or it has to be provided with the access or implementations.
But in an abstract class, it is not mandatory for the property containing an abstract keyword in the function. So we also use the lambda feature for to provide the instance in a single abstract method if the user requires it.
Examples of Kotlin Abstract Class
Given below are the examples of Kotlin Abstract Class:
Example #1
Code:
package one
open class demo {
open fun eg() {
println("Welcome To My Domain its the first example that related to the Kotlin abstract class")
}
}
abstract class demo1 : demo() {
override abstract fun eg()
}
class demo2: demo1(){
override fun eg() {
println("Its a first child class which inherits from the demo which is the parent class")
}
}
class demo3: demo(){
override fun eg() {
println("Its a second child class which inherits from the demo which is the parent class")
}
}
abstract class subject {
abstract fun physics()
}
class sub1 : subject() {
override fun physics(){
val m=60
println(m)
}
}
class sub2 : subject() {
override fun physics(){
val m=70
println(m)
}
}
fun main(args: Array<String>){
val x = demo()
x.eg()
val y = demo2()
y.eg()
val z= demo3()
z.eg()
var p: subject = sub1()
val p1 = p.physics()
println("Thank you users your physics subject mark is $p1")
var p2: subject = sub2()
val p3 = p2.physics()
println("Your physics mark is $p3")
}
Output:
In the above example, we used two abstract classes called demo and subject. In addition, we created the two-child classes for to perform the abstract class operations.
Example #2
Code:
package one
abstract class Employee(empname:String, age:Int){
abstract var id: Int
abstract fun details()
init {
println("Employee name is: $empname")
println("Employee age is: $age")
}
fun exam(){
println("Thank you employees your details are")
}
}
class Employee1(empname:String, age:Int): Employee(empname, age) {
override var id: Int = 3
override fun details() {
println("Thank you employees your details are $id")
}
}
class Employee2(empname:String,age:Int): Employee(empname,age) {
override var id: Int = 4
override fun details() {
println("Thank you employees your details are $id")
}
}
fun main() {
val Employee1 = Employee1("Siva",31)
val Employee2 = Employee2("raman", 30)
println("The Employee1 details are : ${Employee1.id}")
println("The Employee2 details are : ${Employee2.id}")
Employee1.details()
Employee2.details()
Employee1.exam()
Employee2.exam()
}
Output:
In the second example, we used an abstract class for regarding the employee details. We can create the two-child classes like employee1 and employee2 for storing and retrieving the details with the help of the instance.
Example #3
Code:
package one
abstract class Month(monthname:String){
abstract var name: String
abstract fun monthdetails()
init {
println("month name is: $name")
}
fun exam(){
println("Thank you the month name is")
}
}
interface mnths {
var vars: String
fun sample():String
fun sample1() {
println("Have a Nice day users")
}
}
class latestmonth : mnths {
override var vars: String = "May"
override fun sample() = "June"
}
class Month1(monthname:String): Month(monthname) {
override var name: String = "July"
override fun monthdetails() {
println("Thank you users your month is $name")
}
}
fun main() {
val m2 = Month1("May")
println("Your current month is : ${m2.name}")
m2.monthdetails()
val j = latestmonth()
println("Your latest month is = ${j.vars}")
print("Keep on spent the time with our application: ")
j.sample1()
print("Nice day users ")
println(j.sample())
}
Output:
In the final example, we used abstract class and interface; it has the same feature as abstract class. An interface contains a non-abstract method like sample1(); similar to the abstract class, it contains both abstract and non-abstract methods. Finally, the object was created in the main method with the help of the normal class reference, and it calls the method.
Conclusion
The kotlin abstract class is one of the keywords and features for defining and implement the application, and it cannot be instantiated. However, it can be inherited with the multiple classes, and it conflicts to handled such more conflicts based on the scenarios that happened on both the compile and runtime codes.
Recommended Articles
This is a guide to Kotlin Abstract Class. Here we discuss the introduction, syntax, and working of abstract class in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –