ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Inheritance in Kotlin

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='25' AND `tutorial_submenu`='92' AND `tutorial_status`=1 LIMIT 1

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

kotlin

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:

css

Buddy makes a soundBuddy barks loudly

  • Dog class inherits from Animal using the : Animal(name) syntax.

  • The sound() method is accessible in Dog using inheritance.


βœ… 3. Overriding Methods

You can override superclass methods using the override keyword.

πŸ“Œ Example:

kotlin// Superclass

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 the Cat class overrides the one in Animal.


βœ… 4. Calling Superclass Methods

You can call the superclass method using the super keyword.

πŸ“Œ Example:

kotlinopen class Animal {

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:

css

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:

kotlin

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:

pgsql

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:

csharp

Car is driving

  • The Car class provides its own implementation of the drive() 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:

kotlin

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 as final.


βœ… 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.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql