Welcome to the Kotlin Inheritance tutorial! In this lesson, we'll dive deep into one of the fundamental aspects of object-oriented programming (OOP) in Kotlin - Inheritance. We'll explore how to create and use parent-child class relationships, understand the concept of superclasses and subclasses, and see how inheritance can make your code more organized and reusable. š
Inheritance is a mechanism that allows one class (child or subclass) to acquire properties (methods and variables) from another class (parent or superclass). In Kotlin, we can create a subclass by using the : symbol followed by the parent class name.
// Define the parent class
class Animal {
var name: String = ""
fun eat() {
println("The $name is eating.")
}
}
// Create a subclass by inheriting from the Animal class
class Dog : Animal() {
var breed: String = ""
// Override the eat() method in the subclass
override fun eat() {
println("The $name ($breed) is eating.")
}
}In the above example, Dog is a subclass of Animal. It inherits the name property and the eat() method from the Animal class. Additionally, we added a new breed property to the Dog class.
š” Pro Tip: When a subclass inherits a method with the same name from the parent class, we can override the method in the subclass to modify its behavior.
Inheritance relationships can be organized in a hierarchy. A class can inherit from a single parent class, but a parent class can have multiple child classes. Here's an example of an inheritance hierarchy:
class Animal {
// Properties and methods of the Animal class
}
class Mammal : Animal() {
// Properties and methods specific to Mammals
}
class Dog : Mammal() {
// Properties and methods specific to Dogs
}
class Cat : Mammal() {
// Properties and methods specific to Cats
}In this example, Dog and Cat are child classes of the Mammal class, which in turn is a child class of the Animal class. This forms an inheritance hierarchy.
Inheritance is closely related to polymorphism, which is the ability of objects to take on many forms. With polymorphism, we can treat a parent class reference as a child class object. This allows us to call methods and access properties of the child class even though we are using a parent class reference.
Here's an example demonstrating polymorphism with inheritance:
fun main() {
val animal: Animal = Dog()
animal.eat() // Output: The Dog (Breed not set) is eating.
// Set the breed of the Dog
(animal as Dog).breed = "Labrador"
animal.eat() // Output: The Dog (Labrador) is eating.
}In the above example, we declared a variable animal of type Animal but assigned a Dog object to it. This allows us to call the eat() method of the Dog class through the animal reference, even though eat() is not a method of the Animal class. This is an example of polymorphism in action.
Kotlin does not support multiple inheritance from classes directly. However, you can achieve multiple inheritance using interfaces and a class that implements those interfaces.
interface Moveable {
fun move()
}
interface Swimmable {
fun swim()
}
class Dolphin(override val name: String) : Moveable, Swimmable {
override fun move() {
println("The $name is moving.")
}
override fun swim() {
println("The $name is swimming.")
}
}In the above example, we have an interface Moveable and Swimmable. The Dolphin class implements both interfaces and provides the implementation for the methods declared in the interfaces. This allows the Dolphin class to have multiple behaviors (move and swim) without directly inheriting from multiple classes.
Which keyword is used to inherit from a parent class in Kotlin?
That's it for this lesson on Kotlin Inheritance! In the next tutorial, we'll explore interfaces in Kotlin and see how they help us achieve polymorphism. In the meantime, don't forget to practice and experiment with the concepts you've learned in this lesson to solidify your understanding. Happy coding! š»š