I'm not a C# expert but in Java, an abstract class can have fields that are not static or final. They can also some have methods with implementations. One thing of note is that you cannot instantiate an abstract class, you can only subclass it. An interface is a more restricted version of an abstract class. It has no implementation at all. What it does is to define a set of methods that must be implemented in order to make sure that the interface is maintained and consistent across implemented classes. Check out this site from Sun.
What is the difference of an abstract class and an interface in C#?
True, they are similar but not quite exact.
An interface is a contract and defines the requisite behavior of generalization of types.
Abstract classes are concepts.
Here are the differences:
1)An abstract class can contain some implementation. Interfaces have no implementation.
2)Abstract classes can inherit other classes and interfaces. Interfaces can only inherit other interfaces.
3)Abstract classes can contain fields. Interfaces cannot have state.
4)Abstract classes have constructors and destructors. Interfaces have neither.
5)Interfaces can be inherited by structures. Abstract classes are not inheritable by structures.
6)Interfaces support multiple inheritance. Abstract classes support single inheritance
Hope this helps.
Reply:abstract class
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
interface
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Both together
When you create an interface,you are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one inheritance hierarchy and one from the interface.
Reply:Could be wrong, but seeing as c# was born from c++ abstract classes and interfaces are the same in terms of they both can't be instantiated.
But an abstract class can contain some default implementation for some methods, and so you could potentially inherit some sort of behaviour from an abstract class. Also an abstract class can also contain datamembers as well (variables). Only one method has to be declared abstract in an abstract class for the whole class to be abstract.
An interface can not contain any implementation what so ever. It can contain nothing but function prototypes.
With interfaces: you "Implement" an interface in a new class where you have to write definitions for all functions in the interface to be able to create an instance of the class.
With Abstract classes: you "inherit" that class to create a new sub-class, where you have to define functions only for the abstract datamembers to be able to create an instance of that class.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment