Subscribe for notification
Categories: Android Tutorial

Overriding Properties & Functions in Kotlin Child Class

Kotlin Inheritance

Inheritance is the concept of creating class hierarchies wherein we override properties and functions of the base class in its subclasses as per our needs. All classes in Kotlin have a common superclass : Any.

Inheritance allows code reusability. Typically the superclass is known as the base class or the parent class and the subclasses are the derived/child class. Kotlin doesn’t allow a class to inherit from multiple classes because of famous diamond problem.

Explanation:
Here, we have a base class and a derived class. We create an object while instantiating the derived class then it is used to invoke the base class and derived class functions. The derived.A() is used to call the function A() which prints “Base Class” . The derived.B() is used to call the function B() which prints the variable name inherit from the base class and also prints “Derived class”.

  1. We can’t create an object for abstract class.
  2. All the variables (properties) and member functions of an abstract class are by default non-abstract. So, if we want to override these members in the child class then we need to use open keyword.
  3. If we declare a member function as abstract then we does not need to annotatate with open keyword because these are open by default.
  4. An abstract member function doesn’t have a body, and it must be implemented in the derived class.

Use of Inheritance –
Suppose there are three types of Employee in a company a webDeveloper , an iOSDeveloper and an androidDeveloper. All of them have some shared features like name, age and also have some special kind of skills.

First, we create three class individually and all employees have some common and specific skills.

Sponsored
SyncSaS

SyncSaS Technologies is specialized in creating and designing customized software. We have our own team that will be in charge of developing your softwares.

Share
Published by

Recent Posts

Adsense Updated Dashboard

Changes in Adsense Dashboard Recently you might have seen few changes in Adsense dashboard, these…

3 weeks ago

Guide Book For Google Adsense

With my experience in working as AdSense publisher, I have written many articles, tutorials for…

3 weeks ago

How to manage Google Adsense to get High CPC

Manage Adsense for High CPC CPC (Cost per click) in simple words the CPC is…

3 weeks ago

Google Adsense Introduce page level ads

If you are already the publisher of Google Adsense account then now its time to…

3 weeks ago