I have read a lot of logics and examples related to interface. but i never get any satisfied real use of interface. I always work in raw programming.
* One more point people says further implementation is easy using interface ? How is it possible? Suppose an interface is inherit by 100 classes.
Suppose in an example below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterFaceDemo
{
interface IOne
{
void ONE();//Pure Abstract Method Signature
}
interface ITwo
{
void TWO();
}
interface IThree:IOne
{
void THREE();
}
interface IFour
{
void FOUR();
}
interface IFive:IThree
{
void FIVE();
}
interface IEVEN:ITwo,IFour
{
}
class ODDEVEN:IEVEN,IFive//Must Implement all the abstract method, in Derived class.
{
public void ONE()//Implementation of Abstract Method.
{
Console.WriteLine("This is ONE");
}
public void TWO()
{
Console.WriteLine("This is TWO");
}
public void THREE()
{
Console.WriteLine("This is THERE");
}
public void FOUR()
{
Console.WriteLine("This is FOUR");
}
public void FIVE()
{
Console.WriteLine("This is FIVE");
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterFaceDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is ODD");
IFive obj1 = new ODDEVEN();
obj1.ONE();
obj1.THREE();
obj1.FIVE();
Console.WriteLine("\n\nThis is EVEN");
IEVEN obj2 = new ODDEVEN();
obj2.TWO();
obj2.FOUR();
Console.ReadLine();
}
}
}
The following is the output:
Everyone can do same task without interface then what is exact use.
using System;
namespace vaibhav
{
class ODDEVEN
{
public void ONE()
{
Console.WriteLine("This is ONE");
}
public void TWO()
{
Console.WriteLine("This is TWO");
}
public void THREE()
{
Console.WriteLine("This is THERE");
}
public void FOUR()
{
Console.WriteLine("This is FOUR");
}
public void FIVE()
{
Console.WriteLine("This is FIVE");
}
}
public class SametaskWithoutinterface
{
public static void Main()
{
Console.WriteLine("This is ODD");
ODDEVEN obj1 = new ODDEVEN();
obj1.ONE();
obj1.THREE();
obj1.FIVE();
Console.WriteLine("\n\nThis is EVEN");
ODDEVEN obj2 = new ODDEVEN();
obj2.TWO();
obj2.FOUR();
Console.ReadLine();
}
}
}
Both example will gives you same result then why did i choose interface?