In Kotlin, inheritance is a central concept of object-oriented programming. It allows a class to inherit properties and methods from another class, allowing you to extend existing class content or change its behavior.

In the context of inheritance, a distinction is mainly made between: Super class (or base class), which defines basic functionalities, and the Subclass, which inherits these functionalities and can modify or extend them.

Syntax of inheritance in Kotlin

In Kotlin, classes are 'final' by default and therefore not inheritable unless they are explicitly declared as inheritable. To enable a class for inheritance you use the keyword open, which is placed at the beginning of the class declaration:

For an inheriting class, a colon is placed after the class name, followed by the call to the primary constructor of the superclass from which the inheritance occurs:

  • open class MySuperClass { … } – This creates a superclass with the keyword open declared, meaning it can be extended by other classes.
  • class MySubClass: MySuperClass() – In this line a subclass is called MySubClass defined by MySuperClass inherits by calling the primary constructor of the superclass.

Now look at a concrete example:

In this code represents the class Device a general device with the property make (brand/manufacturer) which is standard “Undefined” is, and a method printMake()to output the token.

The class Smartphone is a subclass of Device and thus inherits all properties and methods of the super class Device. This means that an object of the class Smartphone also about the property make and the method printMake() disposes.

When it comes to inheritance, it is important to note that the derived class must call the primary constructor (or, if none exists, the default constructor) of the superclass. 

In our specific case, the class defines Device no primary constructors explicitly. Therefore the class must SmartphoneDerived from Device inherits, the default constructor of the class Device . call

There are basically two ways to call the constructor of a base class in a derived class. The first method is to directly specify the call to the constructor of the base class after the colon.

A second way to invoke the base class's constructor is to define a secondary constructor in the derived class, which is then called using the keyword Great calls the constructor of the base class.

The constructor of Smartphone calls the constructor of the base class Device, indicated by the expression : Excellent() is pictured. In this case, the Device class has a default constructor (since no explicit constructor is defined), and Smartphone calls this constructor.

This means that an object of the class Smartphone all properties and methods of Device inherits. When you create a smartphone object, the first thing you do is create the constructor of the base class Device called to perform the appropriate initializations before any specific initializations for Smartphone be performed.

Regardless of which method is used, we can then use objects of the class Smartphone create and for them the of the class Device use inherited functions.

Inheritance of a class with a primary constructor

If the superclass explicitly defines a constructor (primary or secondary), the subclass must call that constructor. The methods of calling the constructor of the superclass in the derived subclass are the same. 

The first method is to name and call the constructor directly after the class name with a colon.

In this code the class is set Device via the constructor the property make. That's why it's in the class Smartphone defines a constructor that takes a string value and passes it to the constructor of Device hands over.

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked