0% found this document useful (0 votes)
4 views7 pages

11- Abstract class

11- Abstract class

Uploaded by

specsdeveloper13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

11- Abstract class

11- Abstract class

Uploaded by

specsdeveloper13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Trending Now Data Structures Algorithms Topic-wise Practice Python Machine Learning Data Science J

Kotlin Abstract class


Read Discuss Courses Practice

In Kotlin, an abstract class is a class that cannot be instantiated and is meant to


be subclassed. An abstract class may contain both abstract methods (methods
without a body) and concrete methods (methods with a body).

An abstract class is used to provide a common interface and implementation


for its subclasses. When a subclass extends an abstract class, it must provide
implementations for all of the abstract methods defined in the abstract class.

In Kotlin, an abstract class is declared using the abstract keyword in front of


the class. An abstract class can not instantiate means we can not create object
for the abstract class.

Abstract class declaration:

abstract class className {


.........
}

Points to remember:
1. We can’t create an object for abstract class.
2. All the variables (properties) and member functions of an abstract class are
by default non-abstract. So, if we want to override these members in the
child class then we need to use open keyword.
3. If we declare a member function as abstract then we does not need to
annotate with open keyword because these are open by default.
4. An abstract member function doesn’t have a body, and it must be
implemented in the derived class.

An abstract class can contain both abstract and non-abstract members as


shown below:

abstract class className(val x: String) { // Non-Abstract Property

abstract var y: Int // Abstract Property

abstract fun method1() // Abstract Methods

fun method2() { // Non-Abstract Method


println("Non abstract function")
}
}

Kotlin program of using both abstract and non-abstract members in an


abstract class-

Kotlin

//abstract class
abstract class Employee(val name: String,val experience: Int) { // Non-Abstract
// Property
// Abstract Property (Must be overridden by Subclasses)
abstract var salary: Double

// Abstract Methods (Must be implemented by Subclasses)


abstract fun dateOfBirth(date:String)

// Non-Abstract Method
fun employeeDetails() {
println("Name of the employee: $name")
println("Experience in years: $experience")
println("Annual Salary: $salary")
}
}
// derived class
class Engineer(name: String,experience: Int) : Employee(name,experience) {
override var salary = 500000.00
override fun dateOfBirth(date:String){
println("Date of Birth is: $date")
}
}
fun main(args: Array<String>) {
val eng = Engineer("Praveen",2)
eng.employeeDetails()
eng.dateOfBirth("02 December 1994")
}

Output:

Name of the employee: Praveen


Experience in years: 2
Annual Salary: 500000.0
Date of Birth is: 02 December 1994

Explanation: In the above program, Engineer class is derived from the


Employee class. An object eng is instantiated for the Engineer class. We have
passed two parameters to the primary constructor while creating it. This
initializes the non-abstract properties name and experienceof Employee class.
The Then employeeDetails() method is called using the eng object. It will
print the values of name, experience and the overridden salary of the
employee. In the end, dateOfBirth() is called using the eng object and we have
passed the parameter date to the primary constructor. It overrides the abstract
fun of Employee class and prints the value of passed as parameter to the
standard output.

Overriding a non-abstract open member with an abstract one –

In Kotlin we can override the non-abstract open member function of the open
class using the override keyword followed by an abstract in the abstract class.
In the below program we will do it. Kotlin program of overriding a non-
abstract open function by an abstract class –

Kotlin

open class Livingthings {


open fun breathe() {
println("All living things breathe")
}
}
abstract class Animal : Livingthings() {
override abstract fun breathe()
}
class Dog: Animal(){
override fun breathe() {
println("Dog can also breathe")
}
}
fun main(args: Array<String>){
val lt = Livingthings()
lt.breathe()
val d = Dog()
d.breathe()
}

Output:

All living things breathe


Dog can also breathe

Multiple derived classes –

An abstract member of an abstract class can be overridden in all the derived


classes. In the program, we overrides the cal function in three derived class of
calculator. Kotlin program of overriding the abstract function in more than
one derived class –

Kotlin

// abstract class
abstract class Calculator {
abstract fun cal(x: Int, y: Int) : Int
}
// addition of two numbers
class Add : Calculator() {
override fun cal(x: Int, y: Int): Int {
return x + y
}
}
// subtraction of two numbers
class Sub : Calculator() {
override fun cal(x: Int, y: Int): Int {
return x - y
}
}
// multiplication of two numbers
class Mul : Calculator() {
override fun cal(x: Int, y: Int): Int {
return x * y
}
}
fun main(args: Array<String>) {
var add: Calculator = Add()
var x1 = add.cal(4, 6)
println("Addition of two numbers $x1")
var sub: Calculator = Sub()
var x2 = sub.cal(10,6)
println("Subtraction of two numbers $x2")
var mul: Calculator = Mul()
var x3 = mul.cal(20,6)
println("Multiplication of two numbers $x3")
}

Output:

Addition of two numbers 10


Subtraction of two numbers 4
Multiplication of two numbers 120
Division of two numbers 3

Advantages of using abstract classes in Kotlin:

1. Abstraction: Abstract classes provide a way to define a common contract


between different classes without specifying the implementation details.
This enables you to create abstractions that improve the modularity and
maintainability of your code.
2. Polymorphism: Abstract classes allow you to create objects of different
types that have the same interface, which enables polymorphic behavior.
3. Code Reusability: Abstract classes provide a way to reuse code by allowing
multiple classes to extend the same abstract class and share the same
abstract methods and properties.
4. Implementing Common Behavior: Abstract classes provide a way to
implement common behavior for its subclasses, reducing the amount of
code that needs to be written in each subclass.

Disadvantages of using abstract classes in Kotlin:

1. Limited Instantiation: Abstract classes cannot be instantiated, so they


provide limited instantiation details.
2. Complexity: Abstract classes can make your code more complex, especially
if you have many classes that extend multiple abstract classes.

Reference:

A popular book for learning Kotlin is “Kotlin in Action” by Dmitry Jemerov and
Svetlana Isakova. This book provides a comprehensive introduction to the
Kotlin programming language, including its syntax, libraries, and best practices,
with many hands-on examples and exercises.

Last Updated : 09 Feb, 2023 12

Similar Reads
Kotlin Nested class and Inner Kotlin Class and Objects
class

Kotlin | Class Properties and Singleton Class in Kotlin


Custom Accessors
How to Work With Nested How to Iterate Over a Class's
Class in Kotlin? Properties in Kotlin?
How to Get the Class in Restricting Class Hierarchies
Kotlin? in Kotlin

Android - Create Group Kotlin | Language for


BarChart with Kotlin Android, now Official by
Google

Previous Next

Kotlin Sealed Classes Enum Classes in Kotlin

Article Contributed By :
Praveenruhil
P Praveenruhil

Vote for difficulty

Easy Normal Medium Hard Expert

Improved By : m01nak77, ayushpandey3july, sunstar


Article Tags : Kotlin OOPs, Kotlin

Improve Article Report Issue

You might also like