Inheritance is a fundamental object-oriented programming language concept that allows us to inherit properties and functions from another class. In Kotlin, we can only integrate a single inheritance, which means a class can only inherit one superclass. In today’s tutorial, we learn how to implement inheritance in Kotlin class with basic code syntax, superclass, and subclass examples.
Implement Inheritance in Kotlin Class with Examples
What is Inheritance?
In inheritance, a class can inherit functions and properties from another class. There are two classes: the Superclass(Base class) and the Derived class(Subclass).
- Super class(Base class): The class whose functions and properties are inherited into another class is called a Superclass. To make a class accessible for inherited you have to add an open keyword at the time of class declaration.
- Derived class(Subclass): The class that inherits other class functions and properties within is called a Subclass.
Basic Inheritance Syntax in Kotlin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Base class or Superclass open class CommonSchool { open val schoolPrincipalName: String = "Mike" open fun showSchoolName() { println("Kotlin") } } // Derived class or Subclass class School : CommonSchool() { override val schoolPrincipalName = "Jim" override fun showSchoolName() { println("KotlinGuide.com") } } |
In our example, we are inheriting variables and functions into a subclass. Please make sure to add the open keyword to allow inheriting.
Simple Inheritance Example 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// Base class or Superclass open class CommonSchool() { open val schoolPrincipalName: String = "Mike" open fun showSchoolName() { println("School name is KotlinGuide") } open fun showSchoolPrincipalName() { println("School principal name is $schoolPrincipalName") } open fun showSchoolDetails() { println("School details: Learn about Kotlin") } } // Derived class or Subclass class School() : CommonSchool() { override val schoolPrincipalName = "Jim" override fun showSchoolPrincipalName() { println("School principal name is $schoolPrincipalName") } } fun main() { val schoolObject = School() schoolObject.showSchoolName() schoolObject.showSchoolPrincipalName() schoolObject.showSchoolDetails() } // Output // School name is KotlinGuide.com // School principal name is Jim // School details: Learn about Kotlin |
Code explanation:
In our basic inheritance example, we are overriding only 1 variable and 1 function and calling the other 2 functions directly.
Implement Inheritance in Kotlin with properties and methods:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// Base class or Superclass open class CommonSchool(val schoolName: String, val schoolDetails: String) { open val schoolPrincipalName: String = "Mike" open fun showSchoolName() { println("School name is $schoolName") } open fun showSchoolPrincipalName() { println("School principal name is $schoolPrincipalName") } open fun showSchoolDetails() { println("School address is $schoolDetails") } } // Derived class or Subclass class School(schoolName: String, schoolDetails: String) : CommonSchool(schoolName, schoolDetails) { override val schoolPrincipalName = "Jim" override fun showSchoolName() { println("School name is $schoolName") } override fun showSchoolPrincipalName() { println("School principal name is $schoolPrincipalName") } override fun showSchoolDetails() { println("School details: $schoolDetails") } } fun main() { val schoolObject = School("KotlinGuide.com", "Learn everything about Kotlin") schoolObject.showSchoolName() schoolObject.showSchoolPrincipalName() schoolObject.showSchoolDetails() } // Output // School name is KotlinGuide.com // School principal name is Jim // School details: Learn everything about Kotlin |
Code explanation:
We have created a Superclass named CommonSchool with 2 parameters. These parameters are used in member functions of the class. Now we are inheriting this class into School class. We are getting both our superclass parameters in the subclass as parameters and passing them again. In the end, we are creating an instance(object) of our Subclass and calling the superclass functions.