Welcome to The Coding College! In this tutorial, we’ll explore inheritance in Kotlin, a key feature of object-oriented programming that allows one class to derive properties and functions from another. Mastering inheritance is essential for creating scalable and reusable code structures.
What Is Inheritance?
Inheritance allows a class (child class) to acquire properties and methods from another class (parent class). It promotes code reuse and establishes a hierarchical relationship between classes.
In Kotlin, the parent class must be explicitly marked as open
to allow inheritance. By default, classes in Kotlin are final, meaning they cannot be inherited.
Syntax of Inheritance
General Syntax:
open class ParentClass {
// Properties and methods
}
class ChildClass : ParentClass() {
// Additional properties and methods
}
Basic Example of Inheritance
Here’s a simple example of inheritance in Kotlin:
open class Animal {
fun eat() {
println("This animal eats food")
}
}
class Dog : Animal() {
fun bark() {
println("The dog barks")
}
}
fun main() {
val dog = Dog()
dog.eat() // Accessing parent class method
dog.bark() // Accessing child class method
}
// Output:
// This animal eats food
// The dog barks
Overriding Parent Class Functions
Kotlin allows child classes to override functions from the parent class using the override
keyword. The parent function must be marked as open
to allow overriding.
Example:
open class Animal {
open fun sound() {
println("Animal makes a sound")
}
}
class Cat : Animal() {
override fun sound() {
println("Cat meows")
}
}
fun main() {
val cat = Cat()
cat.sound() // Calls the overridden function
}
// Output: Cat meows
Calling the Parent Class Implementation
If needed, you can call the parent class implementation of an overridden function using the super
keyword.
Example:
open class Bird {
open fun fly() {
println("Bird flies")
}
}
class Sparrow : Bird() {
override fun fly() {
super.fly()
println("Sparrow flies at low altitudes")
}
}
fun main() {
val sparrow = Sparrow()
sparrow.fly()
}
// Output:
// Bird flies
// Sparrow flies at low altitudes
Inheritance with Properties
Just like methods, properties in the parent class can also be inherited and overridden.
Example:
open class Vehicle {
open val maxSpeed: Int = 100
}
class SportsCar : Vehicle() {
override val maxSpeed: Int = 300
}
fun main() {
val car = SportsCar()
println("Max Speed: ${car.maxSpeed}")
}
// Output: Max Speed: 300
Using Constructors with Inheritance
When a child class inherits from a parent class, it can pass arguments to the parent class’s constructor using the super
keyword.
Example:
open class Person(val name: String) {
fun displayName() {
println("Name: $name")
}
}
class Student(name: String, val grade: Int) : Person(name) {
fun displayGrade() {
println("Grade: $grade")
}
}
fun main() {
val student = Student("Naman", 12)
student.displayName()
student.displayGrade()
}
// Output:
// Name: Naman
// Grade: 12
Abstract Classes and Inheritance
In Kotlin, abstract classes can be inherited but cannot be instantiated. These classes may contain abstract methods that must be implemented in child classes.
Example:
abstract class Shape {
abstract fun calculateArea(): Double
}
class Circle(val radius: Double) : Shape() {
override fun calculateArea(): Double {
return Math.PI * radius * radius
}
}
fun main() {
val circle = Circle(5.0)
println("Area: ${circle.calculateArea()}")
}
// Output: Area: 78.53981633974483
Why Learn Kotlin Inheritance with The Coding College?
At The Coding College, we emphasize practical learning and building a strong foundation in coding. Mastering inheritance in Kotlin empowers you to write cleaner, more maintainable, and reusable code for large-scale applications.
Conclusion
Inheritance is a cornerstone of object-oriented programming in Kotlin. By understanding and implementing inheritance effectively, you can create robust and extensible code structures.