How do you call the parent implementation of a function from inside an override using `super`, and when is doing so useful?
answer
- `super.foo()` = parent's version
- `this.foo()` would recurse infinitely
- Statically bound to direct superclass
- Position in body is free (not forced first)
- `super@Outer` from inner class
basics
~10 sInside an overriding function, write super.functionName(args) to run the parent's version. It is useful when you want to extend the parent behavior rather than fully replace it.
solid answer
~40 sUse `super.member()` inside an override to invoke the superclass implementation. This lets you *augment* rather than *replace* the inherited behavior — a common pattern in lifecycle methods (e.g. calling `super.onCreate()` first, then adding your own work). `super` resolves to the direct superclass's implementation, bypassing dynamic dispatch for that one call. You can place the `super` call anywhere in the body — before, after, or around your added logic. For properties, `super.propertyName` reads the parent's accessor. Note that `super` always refers to the immediate superclass; to disambiguate between multiple supertypes that each provide a member you use the qualified form `super<TypeName>.member()`. A `super` call is only legal from inside the subclass body, never from outside or from a nested lambda that escapes the class.
code
kotlin · 11 linesopen class View {
open fun draw() = println("draw background")
}
class Button : View() {
override fun draw() {
super.draw() // parent first
println("draw label") // then extra work
}
}
// Button().draw() prints: draw background / draw labelgo deeper
Knows super.foo() runs the parent implementation.
Explains augment-vs-replace, free positioning, and the recursion trap of this.foo().
Notes static binding of super, property accessors, and super@Outer for inner classes.
Discusses template-method/decorator design and the maintainability cost of fragile super-call ordering.
## Calling the parent with `super` Inside an overriding member, the keyword **`super`** gives you a handle to the **direct superclass's implementation** of that member. Writing `super.foo()` runs the parent's `foo`, even though `this.foo()` would dispatch back to your override and cause infinite recursion. ## Why and when Overriding fully *replaces* the parent member. Often you want to *extend* it — keep the parent's work and add your own. `super` is the tool for that: ```kotlin open class Logger { open fun log(msg: String) = println("[base] $msg") } class TimestampLogger : Logger() { override fun log(msg: String) { super.log("${System.currentTimeMillis()} $msg") // reuse parent } } ``` Typical uses: - **Lifecycle/template methods** — `super.onCreate(savedState)` then your setup. - **Decorator-style augmentation** — pre/post-process around the parent call. - **Property accessors** — `super.someProperty` reads the inherited backing accessor. ## Key mechanics - **`super` bypasses dynamic dispatch** for that single call — it is statically bound to the immediate superclass. `this.method()` would re-enter your override. - **Position is free** — call `super` first, last, or in the middle; Kotlin (unlike a Java constructor's implicit `super()`) does not force it first in regular methods. - **Only inside the class body.** You cannot call `super` from outside the object, and you cannot call it from an inner class to reach the outer class's super — for that you use `[email protected]()`. - **Multiple supertypes** providing the same member require the qualified `super<TypeName>.member()` form to pick one. ## Properties `super` works for properties too — `super.name` invokes the parent getter, and within a custom setter you can delegate to the parent accessor where applicable.
- What happens if you call `this.method()` instead of `super.method()` inside an override?It dynamically dispatches back to the same override, causing infinite recursion and a StackOverflowError.
- Can you reach the outer class's superclass from an inner class?Yes, with the qualified form `[email protected]()`, which names the enclosing class whose super you want.
Like a chef following the house recipe (super) then adding their own garnish on top.
saying these in an interview costs you the question
- Thinking `super` calls are dynamically dispatched
- Claiming `super` must always be the first statement
- Using `this.foo()` to call the parent and not seeing the recursion
- Believing `super` can be called from outside the object