How to Call Parent Static Method Using Child Class In Kotlin?

4 minutes read

In Kotlin, you can call a parent static method using a child class by using the super keyword followed by the parent class name and the method name. For example, if you have a parent class called Parent with a static method staticMethod, you can call this method from a child class called Child by using super.Parent.staticMethod(). This allows you to access static methods of the parent class from a child class in Kotlin.


How to call a parent method from a child class in Kotlin?

To call a parent method from a child class in Kotlin, you can use the super keyword followed by the name of the method you want to call.


Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
open class Parent {
    open fun printMessage() {
        println("Hello from Parent class")
    }
}

class Child : Parent() {
    override fun printMessage() {
        super.printMessage() // calling the parent method
        println("Hello from Child class")
    }
}

fun main() {
    val child = Child()
    child.printMessage()
}


In this example, the Child class extends the Parent class and overrides the printMessage method. Inside the overridden method, we use super.printMessage() to call the printMessage method of the parent class and then add our own message.


When you run the program, it will output:

1
2
Hello from Parent class
Hello from Child class



How to use super keyword in Kotlin?

In Kotlin, the super keyword is used to call methods or properties of the superclass.


Here are some examples of how to use the super keyword in Kotlin:

  1. Calling a superclass method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
open class Animal {
    open fun makeSound() {
        println("Animal makes a sound")
    }
}

class Dog : Animal() {
    override fun makeSound() {
        super.makeSound()
        println("Dog barks")
    }
}

fun main() {
    val dog = Dog()
    dog.makeSound()
}


In this example, the Dog class inherits from the Animal class, and the makeSound() method in the Dog class calls the superclass method using the super keyword before printing "Dog barks".

  1. Accessing superclass properties:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
open class Vehicle {
    open val model = "Generic vehicle"
}

class Car : Vehicle() {
    override val model = "Car"
    
    fun printModel() {
        println(super.model) // Prints "Generic vehicle"
        println(model) // Prints "Car"
    }
}

fun main() {
    val car = Car()
    car.printModel()
}


In this example, the Car class overrides the model property of the superclass Vehicle. The printModel() method in the Car class accesses the superclass property using super.model and the subclass property using just model.


These are just a couple of examples of how to use the super keyword in Kotlin. Remember that you can only use the super keyword inside a subclass that is inheriting from a superclass.


How to call a parent class method from a child class in Kotlin?

In Kotlin, you can call a parent class method from a child class using the super keyword followed by the method name. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
open class Parent {
    open fun display() {
        println("This is the parent class")
    }
}

class Child : Parent() {
    override fun display() {
        super.display() // Calling the parent class method
        println("This is the child class")
    }
}

fun main() {
    val child = Child()
    child.display()
}


In this example, the Child class inherits from the Parent class and overrides the display method. Inside the display method of the Child class, we call the parent class method using super.display(). This way, both the parent class method and the child class method will be executed when we call child.display() in the main function.


What is inheritance in Kotlin?

Inheritance in Kotlin refers to the mechanism by which a class can inherit properties and behavior from another class. In Kotlin, classes can be derived from other classes, meaning that a derived class (or subclass) can inherit the members of a base class (or superclass) such as properties and methods. This allows for code reuse and the creation of hierarchical relationships between classes. To inherit from a class in Kotlin, the subclass uses the ": Superclass()" syntax after its name in the class declaration.


What is method overriding in Kotlin?

Method overriding in Kotlin is a feature that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This means that when a subclass extends a superclass and overrides a method, the subclass can provide its own implementation of the method while still maintaining the same method signature as the superclass.


When a method is overridden in a subclass, the subclass implementation of the method will be called instead of the superclass implementation when the method is invoked on an instance of the subclass. This allows for more flexible and customizable behavior in subclasses, while still adhering to the inheritance hierarchy defined by the superclass.

Facebook Twitter LinkedIn

Related Posts:

Pausing and resuming coroutines in Kotlin can be achieved using the suspend keyword along with yield() function.To pause a coroutine, use the yield() function inside a suspend function. This will pause the coroutine and allow other coroutines to run.To resume ...
To get the Minecraft plugin version in Kotlin Maven, you can use the Bukkit API to access the plugin's version information. You can create a listener class that implements the org.bukkit.event.Listener interface and listen for the org.bukkit.event.server.P...
To implement custom text-to-speech in Kotlin, you can start by creating a class or function that handles the text-to-speech functionality. This class or function should utilize the Android TextToSpeech API to convert the text into spoken words.You can customiz...
To use generics as a parameter in Kotlin, you can declare a class, function, or interface with generic type parameters by specifying them within angle brackets (<>). You can then use these generic type parameters to define the type of objects that will b...
To get the button id in Kotlin, you can use the id property of the button view. For example, if you have a button with the id btnSubmit, you can access its id like this: val buttonId = btnSubmit.id This will return the integer value that represents the id of t...