   Abstract classes vs. Interfaces
  I am in the middle of trying to find a ASP.NET programming job in Russia. The question everyone asks is 'Tell me the differences between interfaces and abstract classes and when you would use them'Can anyone briefly explain this and give a brief explanation that I can keep in my head. Since I write ASP.NET web pages I have never needed to really know this answer I understand Interfaces have no methods defined and .. I guess asbstract classes dont also..  But I am tired of sounding stupid when they ask this questions so...Anyone have a good example?? Or a link to a good example ? (I am using C#.NET)   
  Both interfaces and abstract classes enforce a 'contract' on what a class has to actually provide in terms of functionality.  One of the biggest differences is that in C# you can inherit multiple interfaces but only a single class.  Also the fact that an interface is not a class but define the peripheral abilities of a class while an abstract class defines the core identity of a class.An interface does not have any implementation and is only a contract whereas an abstract class can provide various implementation for others to inherit.There are probably around 10 main differences or so between the two and the concepts are completely different.   ;  Can someone give a example when a interface would be needed over a abstract class and vice versa (without using multiple inheritence)  ?;  Abstract classes can have some member functions, method implementations and stuff like normal classes have. Interfaces just define the call interface (and may have enums or static variables). That's all there is to it when you don't consider multiple inheritance..For example you could have an abstract class that has methods:void MyTemplateMethod() {  X x = GetResource();  try {    VirtualMethod(x);  } finally {    x.close();  }}protected virtual void VirtualMethod(X x);The point is that whoever derives from this abstract class must override VirtualMethod to provide the implementation that is called with resource X. But users of this class would call MyTemplateMethod() which does the common task of calling GetResource() and then closing the resource afterwards.You simply couldn't do the above thing with interfaces, because the abstract class doesn't only define interface but also some of the functionality.   ;  A good question.  I've always used Base Classes to inherit from but can see the advantage of being able to utilise multiple interfaces instead of building a class hierarchy.Hopefully someone can give a small example of when an interface is best over a base class.edit:Not sure if I am understanding this correctly but lets see:Abstract ClassUse when some common code but need to enforce overriding of functionsBase ClassUse when there is common code but no need to override functionsInterfaceUse when there is no common code but there is common functionality   ;  It really depends on the situtation.  I mean there is absolutely no reason to use a 'base class' instead of an abstract class if you are never going to instantiate that object, because the abstract class enforces this.  Obviously if your base class in the hierarchy is meant to be instantiated than you wouldn't want it to be abstract.   ;  I think I come confusing abstract class and a virtual class. Or are they the same thingWhat about the difference between a PURELY ABSTRACT CLASS vs. INTERFACE ? Both have no methods defined right?Anyone have any small sample game code I could study that shows the benefits.I have tried to read some design pattern books but you really need to understand interfaces first.Are there any differences between how C# and Java use interfaces and abstract classes ? I am looking for a page or something that I can read over and over until I totally understand this. ;  I think one of your biggest misunderstandings is that an interface is not a class whereas an abstract class is.I googled for the topic and this is the first link:Abstract vs InterfaceIf you can't understand from that I'm not sure what else to say.   ;  Both abstract classes and interfaces define methods. They do not implement them though. Abstract classes can also contain data, but interfaces have no data.Abstract classes are useful when you are trying to create one or more specific versions of an general (or abstract) object.Interfaces are useful when you have two distinctly separate objects that need direct access to one another without having ownership.;Quote:Original post by Anonymous PosterBoth abstract classes and interfaces define methods. They do not implement them though. umm an abstract class can and often does contain implementation for methods.   