Welcome back! In this part of our Kotlin Class Basics Revision, we dive into inheritance, a fundamental concept in object-oriented programming (OOP) with Kotlin. Inheritance allows us to share code across classes, enhancing code organization and efficiency.
In this lesson, we'll explore attribute and method inheritance in Kotlin through practical examples. Our lesson's blueprint includes defining inheritance, examining attribute inheritance, exploring method inheritance, and understanding how to use the super
keyword in Kotlin. Let’s dive in!
Inheritance involves creating a child class that inherits properties and functions from a parent class. In Kotlin, whenever classes share common properties or functions, inheritance becomes a relevant tool.
Here's an example featuring a parent class named Vehicle
and a child class named Car
:
Kotlin1// Define the parent class 'Vehicle' 2open class Vehicle(val color: String, val brand: String) 3 4// Define the child class 'Car', inheriting from 'Vehicle' 5class Car(color: String, brand: String, val doors: Int) : Vehicle(color, brand)
Kotlin supports inheritance through classes, and single inheritance is our focus here, where one parent class serves one child class.
Attribute inheritance allows a child class to inherit the properties of a parent class.
Consider this example featuring a parent class named Artist
, and a child class named Musician
:
Kotlin1open class Artist(var name: String) // Parent class with an attribute 2 3class Musician(name: String, var instrument: String) : Artist(name) // Child inherits parent's attribute 4 5fun main() { 6 val john = Musician("John Lennon", "Guitar") // Creating a Musician instance 7 println(john.name) // Output: John Lennon 8 println(john.instrument) // Output: Guitar 9}
The Musician
class inherits the name
property from the Artist
class while having its own unique property, instrument
.
Similar to properties, method inheritance allows a child class to inherit the functions of a parent class.
In the example below, the Car
class can invoke the start
function from the Vehicle
class:
Kotlin1open class Vehicle(val brand: String) { 2 open fun start() { 3 println("The $brand is starting.") 4 } 5} 6 7class Car(brand: String) : Vehicle(brand) 8 9fun main() { 10 val myCar = Car("BMW") 11 myCar.start() // Output: The BMW is starting. 12}
The super
keyword is essential in inheritance for calling functions from the parent class within the child class, especially useful in method overriding and initialization. It allows a child class to augment or utilize the functionality of a parent class without direct modification.
For instance, when overriding a function to add or alter its behavior, super
enables calling the original function from the parent class to integrate its functionality with new enhancements:
Kotlin1open class Vehicle { 2 open fun start(): String { 3 return "Vehicle is starting..." 4 } 5} 6 7class Car : Vehicle() { 8 override fun start(): String { 9 return super.start() + " Beep! Beep!" 10 } 11} 12 13fun main() { 14 val myCar = Car() 15 println(myCar.start()) // Output: Vehicle is starting... Beep! Beep! 16}
Similarly, during initialization, super
calls the constructor of the parent class, making sure that the child class is initialized properly, adding its specific attributes seamlessly:
Kotlin1open class ParentClass(val value: String) 2 3class ChildClass(value: String, val additionalValue: String) : ParentClass(value) 4 5fun main() { 6 val childClass = ChildClass("value", "additionalValue") 7 println(childClass.value) // Output: value 8 println(childClass.additionalValue) // Output: additionalValue 9}
In these ways, super
facilitates a coherent and modular approach to inheritance by allowing child classes to build upon or adapt the functionality of their parent classes efficiently and cleanly.
We've effectively explored attribute and method inheritance in Kotlin and practiced through various examples. Mastering these concepts can significantly enhance both efficiency and readability in code. Remember, practicing is key to proficiency!
On that note, are you ready for some practice exercises? They'll reinforce your understanding and prepare you for more complex programming tasks. Coding is all about experimenting, learning, and problem-solving. Enjoy the journey!