Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference. Before things start getting difficult let me start using simple and short examples to explain the concept of interfaces. Here's a short example that shows you what an interface looks like.
using System;
interface IDetails
{
void SetDetails(int no, string name);
void GetDetails();
}
class Employee : IDetails
{
string _empName;
int _empID;
public void SetDetails(int empNo, string name)
{
_empName = name;
_empID = empNo;
}
public void GetDetails()
{
Console.WriteLine("Employee Name: " + _empName);
Console.WriteLine("Employee ID: " + _empID);
}
static void Main(string[] args)
{
Employee objEmp = new Employee();
objEmp.SetDetails(20, "John");
objEmp.GetDetails();
}
}
No comments:
Post a Comment