0
0
C Sharp (C#)programming~10 mins

Default interface methods in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default interface methods
Define interface with default method
Implement interface in class
Create instance of class
Call default method via interface
Execute default method code
Output result
The flow shows how an interface with a default method is defined, implemented by a class, and how calling the method runs the default code if not overridden.
Execution Sample
C Sharp (C#)
interface IGreeter {
    void Greet() {
        Console.WriteLine("Hello from default method!");
    }
}

class Greeter : IGreeter {}

IGreeter g = new Greeter();
g.Greet();
This code defines an interface with a default method, implements it in a class without overriding, then calls the default method.
Execution Table
StepActionEvaluationResult
1Define interface IGreeter with default method Greet()Interface created with default methodIGreeter has Greet() with default implementation
2Define class Greeter implementing IGreeterClass created without overriding Greet()Greeter inherits default Greet()
3Create instance g of Greeter as IGreeterInstance createdg is Greeter instance
4Call g.Greet()No override found in GreeterDefault Greet() runs
5Execute default Greet() methodConsole.WriteLine runsOutput: Hello from default method!
💡 Method call completes after default method executes and prints output
Variable Tracker
VariableStartAfter Step 3After Step 4Final
gnullGreeter instanceGreeter instanceGreeter instance
Key Moments - 2 Insights
Why does calling Greet() on Greeter instance run the default method?
Because Greeter does not override Greet(), the call uses the interface's default implementation as shown in execution_table step 4.
Can the class override the default interface method?
Yes, if the class defines its own Greet() method, it will run that instead of the default, replacing the behavior in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when g.Greet() is called?
ACompilation error
B"Hello from default method!"
CNo output
D"Hello from Greeter!"
💡 Hint
See execution_table row 5 where the default method prints the output
At which step does the program create the instance of Greeter?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check execution_table row 3 for instance creation
If Greeter class had its own Greet() method, what would change in the execution?
ACompilation error occurs
BThe default method would still run
CThe class method would run instead of default
DInterface method is removed
💡 Hint
Refer to key_moments about overriding default methods
Concept Snapshot
Default interface methods allow interfaces to provide method bodies.
Classes implementing the interface can use these defaults without overriding.
If a class overrides, its method runs instead.
This helps add new methods to interfaces without breaking existing code.
Call the method via interface reference to use default implementation.
Full Transcript
This example shows how default interface methods work in C#. First, an interface IGreeter is defined with a default method Greet() that prints a message. Then, a class Greeter implements IGreeter but does not override Greet(). When we create an instance of Greeter and call Greet() via the interface reference, the default method runs and prints the message. The execution table traces each step: defining interface and class, creating instance, calling method, and output. The variable tracker shows the instance variable g changes from null to a Greeter object. Key moments clarify why the default method runs and how overriding works. The quiz tests understanding of output, instance creation step, and effect of overriding. The snapshot summarizes the concept simply for quick review.