Professional Documents
Culture Documents
com/
ketkiacharya.net@gmail.com
Ketki Acharya
From: SM VITA ATC of CDAC
ketkiacharya.net@gmail.com
Motivation
The principal motivations for this feature are
•Default interface methods enable an API author to add methods to an interface in future versions without
breaking source or binary compatibility with existing implementations of that interface.
•The feature enables C# to interoperate with APIs targeting Android (Java) and iOS (Swift), which support similar
features.
•As it turns out, adding default interface implementations provides the elements of the "traits" language feature (
https://en.wikipedia.org/wiki/Trait_(computer_programming)). Traits have proven to be a powerful programming
technique (http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf).
•You are allowed to implement indexer, property, or event accessor in the interface.
•You are allowed to use access modifiers like private, protected, internal, public, virtual, abstract, override, sealed, static,
extern with default methods, properties, etc. in the interface. And be careful while using modifier keywords.
•You are allowed to create static fields, methods, properties, indexers, and events in the interface.
•You can override modifiers.
•The explicit access modifiers with default access are public.
•If an interface contains default method and inherited by some specified class, then the class does not know anything about
the existence of the default methods of that interface and also does not contain the implementation of the default method.
•If you override a default method, then there is no need to use any modifier. As shown in example 2.
•You are allowed to use parameters in the default method. As shown in example 3.
•You are allowed to use the same name methods in the interface, but they must have different parameter lists. As shown in
example 3.
•You are allowed to extend the default method.
Historically, there have been several approaches to solve this without duplicating the code in every class
needing the behavior.
Other approaches include multiple inheritance and mixins, but these have drawbacks: the behavior of the code
may unexpectedly change if the order in which the mixins are applied is altered, or if new methods are added to
the parent classes or mixins.
Traits solve these problems by allowing classes to use the trait and get the desired behavior. If a class uses
more than one trait, the order in which the traits are used does not matter. The methods provided by the traits
have direct access to the data of the class.
not contain its definition, whereas the display_2() // This method is only // have its declaration // not its
definition
method contains both declaration and its definition, void display_1();
such type of method is known as default interface // Default method with both // declaration and definition
public void display_2()
Output: methods. {
Console.WriteLine("Hello!! Default Method");
Hello!! Method }
}
Hello!! Default Method
// A class that implements // the I_interface interface.
class Example_Class : I_interface
Now, we call the display_2() method with the help of the {
I_interface interface. If you try to call this method with the class // Providing the body // part of the method
public void display_1()
object {
Console.WriteLine("Hello!! Method");
}
// Calling default method // Main Method
public static void Main(String[] args)
// With the help of Example_Class object { // Creating an object
Example_Class t = new Example_Class();
t.display_2();
t.display_1(); // Calling method
then the compiler will give an error as shown below: I_interface obj = t; // Creating an object
Error CS1061: ‘Example_Class’ does not contain a definition for obj.display_2(); // Calling default method
‘display_2’ and no accessible extension method ‘display_2’ }
}
// A simple interface
interface I_interface Example_Class t = new Example_Class(); // Creating object
{
t.display_1(); // Calling method
// This method having // only declaration // I_interface obj1 = t; // Creating an object
not its definition
obj1.display_2(); // Calling default method
void display_1();
public void display_2() // Default method has A_interface obj2 = t; // Creating an object
both // declaration and definition obj2.display_2();
{
Console.WriteLine("Hello!! Default Method of
I_interface");
}
}
interface A_interface : I_interface
{ // Interface which inherits I_interface
// Here, we override the display_2() method
// Here you are not allowed to use any access modifier
// if you use, then the compiler will give an error
void I_interface.display_2()
{
Console.WriteLine("Hello!! Overriden default
method"); Output:
}
}
class Example_Class : I_interface, A_interface Hello!! Method of I_interface
{ // A class that implements both interfaces. Hello!! Overriden default method
public void display_1() // Providing the body Hello!! Overriden default method
part of the method
{
Console.WriteLine("Hello!! Method of
I_interface");
} USM’s Shriram Mantri Vidyanidhi Info Tech Academy
// C# program to illustrate how // to pass parameters in the// default interface method
using System;
// A simple interface
interface I_interface
{
// Default method with both // declaration and definition // Here, the name of both
methods are same
// but the parameter list is different
public void display_1(int a, int b)
{
int sum;
sum = a + b;
Console.WriteLine("Sum: " + sum);
}
}
}
}
USM’s Shriram Mantri Vidyanidhi Info Tech Academy