Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
Inheritance is one of the key principles of object-oriented programming. It allows for creating a new class based on an existing class, inheriting attributes and behaviors (properties and methods) from the base class.
Kotlin treats inheritance a bit differently than some other popular OOP languages. Here's a tutorial on Kotlin's approach to inheritance:
Unlike Java, in Kotlin, all classes are final
by default, meaning you can't inherit from them. If you want to allow a class to be subclassed, you need to declare it as open
.
open class BaseClass { fun baseFunction() { println("Base function") } }
Once a class is marked open
, you can inherit from it using the :
symbol.
class DerivedClass : BaseClass()
Now, DerivedClass
has access to methods and properties of BaseClass
.
Just as classes are final
by default, so are their member functions and properties. If you want to allow a method or property to be overridden, you need to mark it with the open
keyword in the base class. In the derived class, use the override
keyword.
open class BaseClass { open fun functionToOverride() { println("Base implementation") } } class DerivedClass : BaseClass() { override fun functionToOverride() { println("Derived implementation") } }
You can call the base class's implementation of an overridden method using the super
keyword.
class DerivedClass : BaseClass() { override fun functionToOverride() { super.functionToOverride() println("Additional behavior in derived class") } }
If the base class has a constructor, the derived class must initialize the base class using the primary constructor:
open class BaseClass(val message: String) class DerivedClass(message: String) : BaseClass(message)
If you're using secondary constructors in the derived class, you must either call the primary constructor of the derived class or directly call the base class's constructor using super
.
class DerivedClass : BaseClass { constructor(message: String, anotherParameter: Int) : super(message) { // some initialization } }
Abstract classes are open by default and cannot be instantiated. They can contain abstract methods that don't have an implementation and must be overridden in derived classes.
abstract class AbstractBase { abstract fun abstractFunction(): String } class ConcreteClass : AbstractBase() { override fun abstractFunction() = "Implemented!" }
Kotlin supports multiple inheritances through interfaces. You can declare properties and methods in interfaces, and classes can implement multiple interfaces.
interface InterfaceA { fun functionA() } interface InterfaceB { fun functionB() } class MultiClass : InterfaceA, InterfaceB { override fun functionA() { println("Implement A") } override fun functionB() { println("Implement B") } }
Inheritance in Kotlin ensures more safety and predictability by being explicit about which classes and members can be extended or overridden. This is achieved through the use of the open
, abstract
, and override
keywords. By understanding these concepts, you can design robust Kotlin applications with clear and maintainable object-oriented structures.
Single inheritance in Kotlin:
open class Animal class Dog : Animal()
Subclassing and superclass in Kotlin:
open class Shape class Circle : Shape()
Overriding methods in Kotlin:
open class Shape { open fun draw() { println("Drawing a shape") } } class Circle : Shape() { override fun draw() { println("Drawing a circle") } }
Using 'open' keyword for inheritance in Kotlin:
open
keyword to allow subclasses to inherit and override.open class Shape { open fun draw() { println("Drawing a shape") } }
Inheritance vs composition in Kotlin:
// Inheritance open class Engine class Car : Engine() // Composition class Car(private val engine: Engine)
Constructors in inherited classes in Kotlin:
open class Shape(val color: String) class Circle(color: String, val radius: Double) : Shape(color)
Visibility modifiers in Kotlin inheritance:
private
, protected
, internal
, public
) in inherited members.open class Shape { protected val size = 10 } class Circle : Shape() { fun getSize() = size }
Abstract classes in Kotlin:
abstract class Shape { abstract fun draw() } class Circle : Shape() { override fun draw() { println("Drawing a circle") } }
Interfaces and multiple inheritance in Kotlin:
interface Colorable { fun getColor(): String } open class Shape class ColoredCircle : Shape(), Colorable { override fun getColor(): String { return "Red" } }
When to use inheritance in Kotlin:
open class Animal class Dog : Animal()
Inheritance hierarchy in Kotlin projects:
open class Vehicle class Car : Vehicle() class Bike : Vehicle()