Interfaces in C#
Interfaces can have set of properties,methods,events and indexers but has no implementations. The implementation of each member of the interfaces will be created in the child classes.So we may see all these vividly below.
- An interface defines the signature of properties,methods,events and indexers but not an implementations.
- Interface members are public by default.So don't need to specify access specifiers.
- Access modifiers of the Interface can't be changed.Because it is Internal by default.
- The class must be implemented all the properties,methods,events and indexers of the interface if the class inherits the interface.
- The implementation of the properties,methods,events and indexers in the class must have the access specifier public.
- Object can't be created for an Interface.
(e.g)
using System;
public class Program
{
public static void Main()
{
Child child = new Child();
child.Show();
child.ShowAge();
child.ShowName();
}
}
interface IFather
{
void ShowName();
}
interface IMother
{
void ShowAge();
}
class Child:IMother,IFather
{
public void ShowName()
{
Console.WriteLine("Father function");
}
public void ShowAge()
{
Console.WriteLine("Mother function");
}
public void Show()
{
Console.WriteLine("Child");
}
}
Output
Child
Mother function
Father function
Comments
Post a Comment