Kotlin Inheritance

Introduction

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. The class that inherits is called the subclass (or derived class), and the class being inherited from is called the superclass (or base class). In Kotlin, inheritance allows for code reuse and the creation of hierarchical class structures.

Inheritance in Kotlin

To define a class as inheritable in Kotlin, you need to use the open keyword. By default, classes in Kotlin are final and cannot be inherited. The subclass uses the : symbol followed by the superclass name to indicate inheritance.

Syntax

open class SuperClass {
    // Properties and methods
}

class SubClass : SuperClass() {
    // Additional properties and methods
}

Example

fun main() {
    val student = Student("Rahul", 21, "Computer Science")
    student.displayInfo()
    student.displayMajor()
}

open class Person(val name: String, var age: Int) {
    fun displayInfo() {
        println("Name: $name, Age: $age")
    }
}

class Student(name: String, age: Int, val major: String) : Person(name, age) {
    fun displayMajor() {
        println("Major: $major")
    }
}

Explanation:

  • open class Person(val name: String, var age: Int): Defines an open class Person that can be inherited.
  • class Student(name: String, age: Int, val major: String) : Person(name, age): Defines a subclass Student that inherits from Person.
  • student.displayInfo(): Calls the inherited method displayInfo from the Person class.
  • student.displayMajor(): Calls the displayMajor method defined in the Student class.

Output:

Name: Rahul, Age: 21
Major: Computer Science

Overriding Methods

In Kotlin, you can override methods from the superclass in the subclass using the override keyword. The overridden method must be marked with the open keyword in the superclass.

Syntax

open class SuperClass {
    open fun method() {
        // Method implementation
    }
}

class SubClass : SuperClass() {
    override fun method() {
        // Overridden method implementation
    }
}

Example

fun main() {
    val employee = Employee("Priya", 30, "Software Engineer")
    employee.displayInfo()
}

open class Person(val name: String, var age: Int) {
    open fun displayInfo() {
        println("Name: $name, Age: $age")
    }
}

class Employee(name: String, age: Int, val jobTitle: String) : Person(name, age) {
    override fun displayInfo() {
        println("Name: $name, Age: $age, Job Title: $jobTitle")
    }
}

Explanation:

  • open fun displayInfo() { ... }: Defines an open method displayInfo in the Person class that can be overridden.
  • override fun displayInfo() { ... }: Overrides the displayInfo method in the Employee class.

Output:

Name: Priya, Age: 30, Job Title: Software Engineer

Calling Superclass Methods

You can call a superclass method from an overridden method using the super keyword.

Example

fun main() {
    val manager = Manager("Amit", 40, "IT")
    manager.displayInfo()
}

open class Person(val name: String, var age: Int) {
    open fun displayInfo() {
        println("Name: $name, Age: $age")
    }
}

class Manager(name: String, age: Int, val department: String) : Person(name, age) {
    override fun displayInfo() {
        super.displayInfo()
        println("Department: $department")
    }
}

Explanation:

  • super.displayInfo(): Calls the displayInfo method from the superclass Person.

Output:

Name: Amit, Age: 40
Department: IT

Properties and Inheritance

You can also override properties from the superclass in the subclass using the override keyword. The overridden property must be marked with the open keyword in the superclass.

Syntax

open class SuperClass {
    open val property: Type = value
}

class SubClass : SuperClass() {
    override val property: Type = newValue
}

Example

fun main() {
    val car = Car()
    println(car.brand)
}

open class Vehicle {
    open val brand: String = "Unknown"
}

class Car : Vehicle() {
    override val brand: String = "Toyota"
}

Explanation:

  • open val brand: String = "Unknown": Defines an open property brand in the Vehicle class that can be overridden.
  • override val brand: String = "Toyota": Overrides the brand property in the Car class.

Output:

Toyota

Abstract Classes and Methods

An abstract class cannot be instantiated and can contain abstract methods that must be implemented by subclasses. Abstract methods are defined without a body.

Syntax

abstract class SuperClass {
    abstract fun abstractMethod()
}

class SubClass : SuperClass() {
    override fun abstractMethod() {
        // Method implementation
    }
}

Example

fun main() {
    val circle = Circle(5.0)
    println("Area of circle: ${circle.area()}")
}

abstract class Shape {
    abstract fun area(): Double
}

class Circle(val radius: Double) : Shape() {
    override fun area(): Double {
        return Math.PI * radius * radius
    }
}

Explanation:

  • abstract class Shape { ... }: Defines an abstract class Shape.
  • abstract fun area(): Double: Defines an abstract method area in the Shape class.
  • override fun area(): Double { ... }: Implements the area method in the Circle class.

Output:

Area of circle: 78.53981633974483

Example Program with Inheritance

Here is an example program that demonstrates various aspects of inheritance in Kotlin:

fun main() {
    // Creating instances of Student and Employee
    val student = Student("Rahul", 21, "Computer Science")
    student.displayInfo()
    student.displayMajor()

    val employee = Employee("Priya", 30, "Software Engineer")
    employee.displayInfo()

    // Creating an instance of Manager and calling overridden method
    val manager = Manager("Amit", 40, "IT")
    manager.displayInfo()

    // Creating an instance of Circle and calculating area
    val circle = Circle(5.0)
    println("Area of circle: ${circle.area()}")

    // Using overridden property
    val car = Car()
    println(car.brand)
}

// Basic class with primary constructor
open class Person(val name: String, var age: Int) {
    open fun displayInfo() {
        println("Name: $name, Age: $age")
    }
}

// Class with inheritance and method overriding
class Student(name: String, age: Int, val major: String) : Person(name, age) {
    fun displayMajor() {
        println("Major: $major")
    }
}

// Class with inheritance and method overriding
class Employee(name: String, age: Int, val jobTitle: String) : Person(name, age) {
    override fun displayInfo() {
        println("Name: $name, Age: $age, Job Title: $jobTitle")
    }
}

// Class with inheritance and calling superclass method
class Manager(name: String, age: Int, val department: String) : Person(name, age) {
    override fun displayInfo() {
        super.displayInfo()
        println("Department: $department")
    }
}

// Abstract class and method implementation
abstract class Shape {
    abstract fun area(): Double
}

class Circle(val radius: Double) : Shape() {
    override fun area(): Double {
        return Math.PI * radius * radius
    }
}

// Overriding properties
open class Vehicle {
    open val brand: String = "Unknown"
}

class Car : Vehicle() {
    override val brand: String = "Toyota"
}

Output:

Name: Rahul, Age: 21
Major: Computer Science
Name: Priya, Age: 30, Job Title: Software Engineer
Name: Amit, Age: 40
Department: IT
Area of circle: 78.53981633974483
Toyota

Conclusion

In this chapter, you learned about inheritance in Kotlin, including how to define subclasses, override methods and properties, call superclass methods, and use abstract classes and methods. Inheritance allows for code reuse and the creation of hierarchical class structures, which are essential concepts in object-oriented programming. Understanding and applying inheritance is crucial for writing robust and maintainable Kotlin programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top