
Inheritance in Kotlin
π Inheritance in Kotlin
Inheritance in Kotlin allows a class to acquire the properties and methods of another class. It is a fundamental concept of Object-Oriented Programming (OOP) that promotes code reuse and logical structuring.
β 1. Key Concepts of Inheritance in Kotlin
Superclass (Parent) β The class whose properties and functions are inherited.
Subclass (Child) β The class that inherits from the superclass.
open Keyword β By default, classes in Kotlin are final (cannot be inherited). Use the
open
keyword to allow inheritance.Override β Methods from the superclass can be overridden using the
override
keyword.
β 2. Basic Inheritance Example
π Superclass and Subclass
open class Animal(val name: String) { fun () { println("$name makes a sound") }}class Dog(name: String) : Animal(name) { fun () { println("$name barks loudly") }}fun () { val dog = Dog("Buddy") dog.sound() // From superclass dog.bark() // From subclass}
Output:
Buddy makes a soundBuddy barks loudly
Dog
class inherits fromAnimal
using the: Animal(name)
syntax.The
sound()
method is accessible inDog
using inheritance.
β 3. Overriding Methods
You can override superclass methods using the override
keyword.
π Example:
open class Animal { open fun () { println("Animal makes a sound") }}// Subclass overriding sound() methodclass Cat : Animal() { override fun () { println("Cat meows") }}fun () { val cat = Cat() cat.sound() // Output: Cat meows}
The
sound()
method in theCat
class overrides the one inAnimal
.
β 4. Calling Superclass Methods
You can call the superclass method using the super
keyword.
π Example:
open fun () { println("Animal makes a sound") }}class Dog : Animal() { override fun () { super.sound() // Calls parent method println("Dog barks") }}fun () { val dog = Dog() dog.sound()}
Output:
Animal makes a soundDog barks
super.sound()
calls the parent class's method before executing the overridden code.
β 5. Inheritance with Properties
Properties can also be inherited and overridden using the override
keyword.
π Example:
open class Shape { open val name: String = "Unknown Shape"}class Circle : Shape() { override val name: String = "Circle"}fun () { val circle = Circle() println("Shape Name: ${circle.name}")}
Output:
Shape // Abstract classabstract class Vehicle { abstract fun ()}// Subclass implementing the abstract methodclass Car : Vehicle() { override fun () { println("Car is driving") }}fun () { val car = Car() car.drive()}
Output:
Car is driving
The
Car
class provides its own implementation of thedrive()
method.
β 7. Final Keyword
If you donβt want a class or method to be inherited or overridden, you can use the final
keyword.
π Example:
open class Device { final fun operate() { println("Operating device") }}class Mobile : Device() { // fun operate() {} // Error: Cannot override final method}
operate()
cannot be overridden because it is marked asfinal
.
β Conclusion
Use
open
to allow inheritance.Use
override
to customize methods or properties in the subclass.Use
super
to access parent class methods.Use
abstract
for base classes that need implementation in derived classes.Use
final
to prevent further inheritance or method overriding.