Sobes.tech
Junior — Middle

How to access the methods and properties of a parent class in a programming language using the keyword super?

sobes.tech AI

Answer from AI

In programming languages such as Java and Kotlin, the keyword super is used to refer to methods and properties of the parent class from a subclass. This is useful when you need to call an overridden method of the parent or access its fields.

Example in Java:

class Parent {
    void show() {
        System.out.println("Parent method");
    }
}

class Child extends Parent {
    void show() {
        super.show(); // call to parent method
        System.out.println("Child method");
    }
}

In this example, super.show() calls the show() method from the Parent class.

In Kotlin:

open class Parent {
    open fun show() {
        println("Parent method")
    }
}

class Child : Parent() {
    override fun show() {
        super.show() // call to parent method
        println("Child method")
    }
}

Thus, super allows explicit access to the parent class's implementation.