Inheritance is one of the core concepts in Object-Oriented Programming (OOP). It allows a class to inherit properties and functions from another class, enabling code reuse and extension. Kotlin supports single inheritance, where a class can inherit from only one base class.
Letβs dive into how inheritance works in Kotlin!
In Kotlin, a class can inherit properties and functions from another class. The class that is inherited from is called the superclass, and the class that inherits from it is called the subclass.
To inherit a class in Kotlin, you use the :
symbol. The subclass will automatically inherit the constructor and properties of the superclass unless you override them.
open class Animal(val name: String) { fun speak() { println("$name makes a sound") } } class Dog(name: String) : Animal(name) { fun bark() { println("$name barks!") } } fun main() { val dog = Dog("Buddy") dog.speak() // Inherited function dog.bark() // Function specific to Dog }
In the example above:
speak
method.bark
method.Sometimes, you may want to modify or replace a method in the superclass. This can be done using the override
keyword.
open class Animal(val name: String) { open fun speak() { println("$name makes a sound") } } class Dog(name: String) : Animal(name) { override fun speak() { println("$name barks loudly!") } } fun main() { val dog = Dog("Buddy") dog.speak() // Overridden function }
In this example:
speak
method in Animal is overridden by the Dog class.override
keyword ensures that you are intentionally replacing the superclass method.If you want to call the method of the superclass within the subclass, you can use the super
keyword.
open class Animal(val name: String) { open fun speak() { println("$name makes a sound") } } class Dog(name: String) : Animal(name) { override fun speak() { super.speak() // Calling superclass method println("$name barks loudly!") } } fun main() { val dog = Dog("Buddy") dog.speak() // Calls both superclass and subclass method }
In this example, the super.speak()
call invokes the original speak method from the Animal class before executing the subclass's behavior.
An abstract class cannot be instantiated on its own and is meant to be inherited by other classes. Abstract methods in an abstract class must be overridden in the subclass.
abstract class Animal(val name: String) { abstract fun speak() // Abstract method } class Dog(name: String) : Animal(name) { override fun speak() { println("$name barks loudly!") } } fun main() { val dog = Dog("Buddy") dog.speak() // Calls overridden function }
Here:
Animal
class is abstract and contains an abstract method speak
.Dog
class inherits from Animal
and must override the speak
method.Here are some challenges to get hands-on experience with inheritance:
area()
. Create subclasses like Circle and Rectangle, each implementing the area()
method to calculate its area.role()
.startEngine()
. Inherit it in subclasses like Car and Truck and override startEngine()
in each subclass with unique behavior.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!