How to Implement Inheritance In Kotlin?

10 minutes read

In Kotlin, inheritance can be implemented by using the open keyword on the base class and the override keyword on the derived class. Here's an example of how to implement inheritance in Kotlin:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
open class Vehicle {
    open fun accelerate() {
        println("Vehicle accelerating")
    }

    fun stop() {
        println("Vehicle stopped")
    }
}

class Car : Vehicle() {
    override fun accelerate() {
        println("Car accelerating")
    }

    fun drift() {
        println("Car drifting")
    }
}

fun main() {
    val car = Car()
    car.accelerate() // Output: Car accelerating
    car.stop() // Output: Vehicle stopped
    car.drift() // Output: Car drifting
}


In this example, we have a base class called Vehicle which has an accelerate() function marked as open. This allows the function to be overridden in the derived class. The stop() function is not marked as open, so it cannot be overridden.


The Car class is derived from the Vehicle class using the : symbol. It overrides the accelerate() function and adds a new function called drift(). The accelerate() function in Car prints "Car accelerating" instead of the default implementation in Vehicle.


In the main() function, we create an instance of Car and call its functions. The output demonstrates that the overridden accelerate() function from Car is invoked, while the stop() function remains the same as the one in the base class. Additionally, the drift() function of Car is accessible.

Best Kotlin Books to Read in 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Kotlin in Action

Rating is 4.9 out of 5

Kotlin in Action

3
Java to Kotlin: A Refactoring Guidebook

Rating is 4.8 out of 5

Java to Kotlin: A Refactoring Guidebook

4
Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications

Rating is 4.7 out of 5

Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications

5
Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

Rating is 4.6 out of 5

Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

6
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.5 out of 5

Kotlin Cookbook: A Problem-Focused Approach

7
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Rating is 4.4 out of 5

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

8
Kotlin and Android Development featuring Jetpack: Build Better, Safer Android Apps

Rating is 4.3 out of 5

Kotlin and Android Development featuring Jetpack: Build Better, Safer Android Apps


How to call the parent class constructor in Kotlin?

In Kotlin, you can call the parent class constructor using the super keyword. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
open class ParentClass(val value: Int) {
    // Parent class constructor
    init {
        println("Parent Class: $value")
    }
}

class ChildClass(value: Int) : ParentClass(value) {
    // Child class constructor
    init {
        println("Child Class: $value")
    }
}

fun main() {
    val child = ChildClass(10)
}


In the above example, the ChildClass extends the ParentClass. To call the parent class constructor, you need to pass the arguments to the super keyword in the child class constructor ChildClass(value: Int) : ParentClass(value). The parent class constructor will be called first followed by the child class constructor.


How to access properties and methods of the parent class in Kotlin?

In Kotlin, you can access properties and methods of the parent class using the super keyword.


To access a property of the parent class, you can use the super.propertyName syntax. To access a method of the parent class, you can use the super.methodName() syntax.


Here's an example demonstrating the use of super:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
open class ParentClass {
    var property: String = "Parent property"

    open fun method() {
        println("Parent method")
    }
}

class ChildClass : ParentClass() {
    override fun method() {
        super.method() // accessing parent method
        println("Child method")
        println(super.property) // accessing parent property
    }
}

fun main() {
    val child = ChildClass()
    child.method()
}


Output:

1
2
3
Parent method
Child method
Parent property


In the example above, ChildClass inherits from ParentClass and overrides the method() function. Inside the overridden method() function of ChildClass, super.method() is used to access the parent class's method() and super.property is used to access the parent class's property.


What is an abstract class in Kotlin?

In Kotlin, an abstract class is a class that cannot be instantiated but can be inherited by other classes. It is designed to serve as a blueprint for other classes by providing common attributes and methods.


An abstract class can have both abstract and non-abstract (concrete) methods. Abstract methods are declared without an implementation and must be overridden by the subclasses. Non-abstract methods can have an implementation and can be used directly by the subclasses.


To define an abstract class in Kotlin, the "abstract" keyword is used before the class declaration. It can be inherited by using the "override" keyword followed by the implementation of abstract methods in the subclass.


Overall, an abstract class allows for code reuse and provides a common structure to the subclasses while allowing customization through method overriding.


How to implement inheritance in Kotlin?

In Kotlin, inheritance can be implemented using the ":" symbol after the class name followed by the superclass from which you want to inherit. Here is an example of how to implement inheritance in Kotlin:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
open class Animal {
    open fun sound() {
        println("Animal makes a sound")
    }
}

class Dog : Animal() {
    override fun sound() {
        println("Dog barks")
    }
}

fun main() {
    val dog = Dog()
    dog.sound()
}


In this example, the Animal class is the superclass, and the Dog class is the subclass inheriting from Animal. The open keyword is used to declare that the Animal class can be inherited, and the open keyword is also used for the sound function in the Animal class to indicate that it can be overridden in the subclass.


The Dog class uses the : Animal() syntax after its name to indicate that it inherits from the Animal class. The sound function in the Dog class overrides the sound function in the Animal class using the override keyword.


In the main function, an instance of the Dog class is created, and the sound function is called. The output of this example will be "Dog barks" because the sound function in the Dog class overrides the implementation of the sound function in the Animal class.


How to define a child class in Kotlin?

In Kotlin, you can define a child class by using the : symbol followed by the name of the parent class. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Parent class
open class ParentClass {
    open fun printMessage() {
        println("This is the parent class")
    }
}

// Child class
class ChildClass : ParentClass() {
    override fun printMessage() {
        println("This is the child class")
    }
}

fun main() {
    val child = ChildClass()
    child.printMessage() // Output: This is the child class
}


In the example above, the ChildClass inherits from the ParentClass. The override keyword is used to override the printMessage() method from the parent class, allowing the child class to provide its own implementation.


What is an interface in Kotlin?

In Kotlin, an interface is a collection of abstract methods and properties that can be implemented by classes. It defines a contract that classes must adhere to by providing the implementation for the defined methods and properties.


An interface in Kotlin can include both abstract methods (methods without an implementation) and default methods (methods with a default implementation). It can also include abstract properties (properties without an implementation) and constants.


Classes can implement one or more interfaces by overriding the abstract methods and properties defined in the interface. This allows multiple inheritance, as a class can implement multiple interfaces. By implementing an interface, a class guarantees that it will provide the implementation for all the methods and properties defined in the interface.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To reformat a list of items from Lua to Kotlin, you can create a new list in Kotlin and iterate through each item in the Lua list, adding them to the new Kotlin list. You will need to convert each item from Lua data type to Kotlin data type as needed. Also, ma...
To install the Kotlin compiler, you can follow these steps:Firstly, ensure that you have Java Development Kit (JDK) installed on your system. Kotlin requires JDK version 6 or higher to run. Visit the official Kotlin website at kotlinlang.org and navigate to th...
Kotlin is a programming language developed by JetBrains, designed to be a more concise and expressive alternative to Java for Android development. Here is a general overview of how to use Kotlin for Android development:Installation: Start by installing the Kot...