Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Why do we use interfaces; why not to use classes instead of interfaces as there is only declaration in interface?

user-image
Question ajoutée par Atif Gulzar , Web Developer , Ethos Integrated Solutions
Date de publication: 2013/10/07
Deepak Mishra
par Deepak Mishra , Lead Developer , Chevron

Check this code and reply if you got the answer:

interface Iperson   

{        int Subscribe();   

}   

class Program  

  {        static void Main(string[] args)       

{            List pl = new List();           

Iperson person1 = new Customer();           

Iperson person2 = new Employee();           

pl.Add(person1);           

pl.Add(person2);           

foreach (Iperson i in pl)              

Console.WriteLine( i.Subscribe());           

Console.Read();      

  }   

}   

class Customer : Iperson   

{       

public int Subscriber { get; private set; }       

public int Subscribe()       

{           

Subscriber =0;           

return Subscriber;

}   

}   

class Employee : Iperson   

{       

public int Subscriber { get; private set; }       

public int Subscribe()      

 {

Subscriber =1;

return Subscriber;

}   

}

Samuel Eid Safwat Naguib
par Samuel Eid Safwat Naguib , Technology lead ,Project Manager ,Transformation Coach , Octalpha

Interfaces don't support implementation, so you cannot supply any default implementations as you can with abstract classes. Additionally, interfaces are not restricted to hierarchies, so they are more flexible than abstract classes.

More Questions Like This