Skip to main content
infervour.com

Back to all posts

How to Implement Inheritance In Kotlin?

Published on
6 min read
How to Implement Inheritance In Kotlin? image

Best Kotlin Programming Books to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
3 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.21 $49.99
Save 8%
Functional Programming in Kotlin
4 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$48.35 $79.99
Save 40%
Head First Kotlin: A Brain-Friendly Guide
5 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
6 Kotlin Programming: Learning Guide Covering the Essentials and Advancing to Complex Concepts

Kotlin Programming: Learning Guide Covering the Essentials and Advancing to Complex Concepts

BUY & SAVE
$24.99
Kotlin Programming: Learning Guide Covering the Essentials and Advancing to Complex Concepts
7 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
8 Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

BUY & SAVE
$47.99
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)
9 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$33.99 $44.99
Save 24%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
10 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

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:

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.

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:

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:

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:

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:

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:

// 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.