hi friends
what is the difference between interface and abstract class? please justify in which case we use interface and in which case we use abstract class .
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Nov 14, 2011, 12:24 AM
Abstract Class:-
An abstract method is created by specifying the abstract type modifier.
An abstract method contains no body.
An abstract method is not implemented by the base class.
An abstract method is automatically virtual.
A derived class must override it.
Abstract class can have modifiers for methods,properties etc.,
An abstract class can implement a property.
The abstract modifier cannot be applied to static methods.
Properties can also be abstract.
A class containing abstract methods must be declared as abstract with the abstract specifier.
There can be no objects of an abstract class.
If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.
An abstract class can inherit from a class and one or more interfaces.
An abstract class can implement code with non-Abstract methods.
Abstract class can have constant and fields.
An abstract class can have constructors or destructor's.
An abstract class cannot be inherited from by structures.
An abstract class cannot support multiple inheritance.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Interface:-
Interfaces cannot be instantiated directly.
Interfaces can contain events, method, indexer and properties.
An Interface can contain property definitions.
Interfaces contain no implementation of methods.
Classes and Structs can implement more than one interface.
An Interface can be inherited from by structures.
An interface itself can inherit from multiple interfaces (Interface can support multiple
inheritance).
An abstract class can implement a property.
If we add a new method to an Interface then we have to track down all the implementations of
the interface and define implementation for the new method.
Milan ZipancicPosted Nov 14, 2011, 8:13 AM
- Do you want to inherit multiple classes? It is not possible in C#, but one class can implement more interfaces at the same time.
- Class "does something", interface "represents something". Interfaces could be independent from what "class does".
- If many classes inherit form one base (abstract) class, you will be fillled with too many tiny functionalities.
Use abstract class:
- Is there a reasonable amount of common functionalities and data to use in all subclasses? If there is, use absract base class. If you use same interface in multiple classes, you might have case of a code repeatation.
- Abstract class allows you to define some methods with not just theirs signature (access modifier, return type, method name, parameters), but also with body. So, full declaration is possible. Actually, you can provide full declaration for all methods but one. You will have to define at least one method declaration with no body defined for it, just siganture (as for interfaces). In this way, comparing to interface, abstract class can force a specific method implementation and this can be useful sometimes. With interface you have no control over method implementation.
Satyapriya NayakPosted Nov 13, 2011, 8:22 AM
Please refer Our recommended articles.
Thanks
VulpesPosted Nov 13, 2011, 7:19 AM
http://www.c-sharpcorner.com/Forums/Thread/110425/what-is-abstract-class-and-interface-and-where-exactly-s.aspx