Greetings! Today, we're going to demystify the crucial terms of Object-oriented programming (OOP): Inheritance and Polymorphism. These concepts form the backbone of efficient OOP. Our journey will unfold as follows: we'll start with an intuitive grasp of Inheritance and its implementation in Kotlin, and then we'll delve into Polymorphism, with a special focus on method overriding.
Inheritance is akin to repurposing an old blueprint to create something new. In OOP, it allows one class to inherit features (properties and functions) from another.
In Kotlin, we have the Parent Class (also known as Superclass), which provides features, and the Child Class (or Subclass), which receives these features. When implementing, we use the open
keyword for the Parent Class (to allow inheritance), and the :
symbol for the Child class indicates which Parent class the features are coming from.
Inheritance in Kotlin involves subclasses inheriting features from their superclass. A crucial part of this process is ensuring that constructors in the superclass are properly invoked by the subclass. Constructors are special methods used to initialize new objects, and when a class inherits from another, the subclass must initialize the superclass as well, often providing the necessary parameters for any superclass constructor.
