Techoceanhub logo

Inheritance in Kotlin – (Chapter-8)

We have worked with inheritance concepts in Java, c#, and many other languages, but now let’s see how the inheritance concept works with Kotlin. By definition, we all know that inheritance means acquiring some or whole properties of the parent class into the child class.

All classes in Kotlin have a common superclass Any. Like all other OOPs languages, Kotlin also provides this functionality using ” : ” which helps in inheriting child class from the base class.

class Example // Implicitly inherits from Any, we don't have to declare it.

By default, Kotlin classes are final, they can’t be inherited therefore we need to use the keyword “open” in front of the class declaration to make it inheritable just like we use public in case of inheritance in Java. Have a look at the below-mentioned code.

open class Parent //Class is allowed for inheritance

Let us see a below simple code which shows how inheritance will work:-

open class Parent {
    open val vertexCount: Int = 0
}

class Child : Parent() {
    override val vertexCount = 4
}

The concept of inheritance allows code reusability. A derived class has only one base class and can have multiple interfaces, whereas a base class may have one or more child classes.

This is a basic implementation of inheritance in Kotlin, in next upcoming post we will be getting in depth of inheritance in kotlin.

Feel free to read and comment below.

Below is my android application, and is useful for people who has Google Opinion Rewards to cash out.

https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash

If you wanted to create a website, please visit my fiverr gig link below or contact me on support@techoceanhub.com.

https://www.fiverr.com/share/EdZ9L7

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .