Thursday, July 9, 2009

What is the difference between an interface and an abstract class?

i'm programming in C# if that makes any difference

What is the difference between an interface and an abstract class?
Based on Java and C++ my guess is:





Abstract class; one or more methods are left undefined requiring the inheriting classes to implement the specific definitions for those methods.





Interface; Defines a specific set of method prototypes that will be defined in the inheriting class. In essence an interface is a fully abstract class (all methods implementations are left to the developer).





In Java, interfaces are used to add to a class definition without the requirement of true object inheritance (since Java in single inheritance). If c# follows a similar design, then interfaces allow for a level of object abstraction without requiring the user to inherit mutiple classes. In a way, an interface acts as a "promise" to other objects that this class will implement said methods.
Reply:Knowing that an interface can only contain "method signatures" is just the nuts and bolts.





Interfaces are very powerful precisely BECAUSE they limit you:





Abstract classes are used for "vertical" organization: refactoring code implementations, creating object hierarchies *within* packages of classes.





Interfaces, on the other hand, are for "horizontal" organization: For defining narrow interfaces between components and services.





For example, consider the Swing TableModel interface - this is an excellent example. It has just a few simple methods... and many classes (including your own!) Could be made to work with a "table model". Yet, behind this interface, a totally unrelated set of code can "implement" it, and supply its data.





See "design patterns" ... "adapter".
Reply:An abstract class is a class that contains a pure virtual method or function(inc c++). In C#, you do not need a pure virtual method to make a class abstract, just the keyword. Essentially when you make a class abstract, you are saying that you do not want any specific instances of it made, but the class can be used for inheritance. C# only supports single class inheritance. Also, with an abstract class it can contain other non pure virtual methods that provide functionality. For example, I can make an abstract class named Shape with a property to get and set the name and implement an abstract method to return the number of vertexes for that shape. I can't make a shape object, but I can inherit from it to make a square. I do not need to implement the name method because it is already defined, but I do need to implement the vertexes one because it is a pure virtual method.





Interfaces can be thought of as an abstract class that only contains pure virtual methods; however, in C# you can implement multiple interfaces to produce certain behaviors. You still need to implement all of the pure virtual methods, but you can use an interface and another class to represent a different "is-a" relationship.





Another thing to note, the class in C# must be the first thing after the : if you are inheriting. For example,





// Here Square inherits from the class Shape and implements


// the interfaces IEnumerable and IClonable





class Square:Shape, IEnumerable, IClonable


{


...


}


No comments:

Post a Comment