Inside an overriding method, how do you call a specific parent's implementation when several supertypes are involved? Show the exact syntax and explain how the target is chosen.
answer
- super<Type>.member() = angle-bracket qualifier
- Must be a direct supertype that declares it
- Statically bound, not virtual
- Works for property accessors too
- Don't confuse with [email protected]()
basics
~10 sUse super<TypeName>.method(). The name in angle brackets is the exact parent whose version you want to run. You can call several parents this way, in any order, from inside your override.
solid answer
~40 sThe qualified super call is super<SupertypeName>.member(...). The angle-bracket name must be a **direct supertype** of the current class that actually declares (or provides) that member. The compiler resolves the call statically to that supertype's implementation — it is not virtual dispatch. You can chain multiple such calls (super<A>.foo(); super<B>.foo()) to combine behaviors. It works for both functions and property accessors. If you write plain super.member() and the member is provided by more than one supertype, it's an error: 'Many supertypes available, please specify the one you mean in angle brackets'. The qualifier removes that ambiguity. Note the target type name must be the supertype that declares the member, not an unrelated type.
code
kotlin · 6 linesinterface A { fun f() = "A" }
interface B { fun f() = "B" }
class C : A, B {
override fun f() = super<A>.f() + super<B>.f() // "AB"
}
fun main() { println(C().f()) }go deeper
Recognizes the super<Type>.foo() syntax exists and is for picking a parent.
Writes it correctly, chains multiple calls, and applies it to property accessors.
Explains static (non-virtual) resolution and the constraint that the qualifier must be a declaring direct supertype.
Distinguishes the diamond qualifier from outer-class super@Label qualification and reasons about resolution rules precisely.
## Syntax ```kotlin super<Supertype>.member(args) ``` The **`super`** keyword refers to a parent; the **`<Supertype>`** qualifier names *which* parent. This is the only way to disambiguate when several supertypes offer the same member. ## How the target is chosen - The qualifier must name a **direct supertype** of the enclosing class. - That supertype must actually **provide or declare** the member you're calling. - Resolution is **static**: `super<A>.foo()` is bound at compile time to `A`'s implementation. It is *not* polymorphic — no further override down the chain is consulted. ```kotlin interface Greeter { fun greet() { println("hi") } } interface Logger { fun greet() { println("log: greet") } } class Service : Greeter, Logger { override fun greet() { super<Greeter>.greet() // -> "hi" super<Logger>.greet() // -> "log: greet" } } ``` ## Works for properties too If two interfaces provide a default property getter, the same qualifier applies to the accessor: ```kotlin interface X { val tag: String get() = "X" } interface Y { val tag: String get() = "Y" } class Z : X, Y { override val tag: String get() = super<X>.tag + super<Y>.tag // "XY" } ``` ## Errors you'll hit - Plain `super.greet()` when both supertypes have it -> *"Many supertypes available, please specify the one you mean in angle brackets"*. - `super<SomethingNotASupertype>.greet()` -> *"Not a supertype"*. - Qualifying with a supertype that doesn't declare the member -> unresolved reference. ## Outer-class qualification (related) A different but visually similar form, `[email protected]()`, is used from an inner class to reach the outer class's super — don't confuse the `<Type>` (diamond) qualifier with the `@Label` (outer-class) qualifier.
- Is super<A>.foo() virtual or statically resolved?Statically resolved to A's implementation at compile time; it does not dispatch to further overrides.
- Can you use this with property getters?Yes — super<A>.someProperty calls A's getter; the angle-bracket qualifier applies to accessors the same way.
- What's the difference between super<A>.foo() and [email protected]()?The first selects a supertype in a diamond; the second, from an inner class, reaches the enclosing outer class's super-call.
saying these in an interview costs you the question
- Writing super(A).foo() (Java/other syntax) instead of super<A>.foo()
- Believing the qualified call is polymorphic
- Qualifying with a type that isn't a direct supertype
- Confusing super<Type> with super@Label
- Thinking it only works for functions, not properties