ASP.NET - Why use an abstract class instead of an interface?

Asked By myzonal.com myzonal.com on 13-Jun-12 06:44 AM
Why use an abstract class instead of an interface?
kalpana aparnathi replied to myzonal.com myzonal.com on 13-Jun-12 06:45 AM
hi,

Use this referance article:

http://www.thecoldsun.com/en/content/01-2009/abstract-classes-and-interfaces

Regards,
bharti odedra replied to myzonal.com myzonal.com on 13-Jun-12 06:50 AM

An interface is a collection of public methods and properties that are grouped together to encapsulate specific functionality.After an interface has been defined,you can implement it in a class. This means that the class will then support all of the properties and members specified by the interface.

   A class can support multiple interfaces,and multiple classes can support the same interface.The concept of an interface,therefore,makes life easier for users and other developers.For example, you might have some code that uses an object with a cetain interface. Provided that you don't use other properties and methods of this object,it is possible to replace one object with another interface.
   Once an interface is published-that is,it has been made available to other developers or end users-it is good practice not to change.one way of thinking about this is to imagine the interface as a contract between class creator and class consumers.

Syntax
Interface<interface name>
{
        //declaration of method and variable

    The access modifier keyword public and internal are used in the same way,and as with classes,interfaces are defined as internal by default.
    The keyword abstract and sealed are not allowed because neither modifier makes sense in the context of interfaces

TSN ... replied to myzonal.com myzonal.com on 13-Jun-12 07:01 AM
hi...

if you want any impementation in the base class then you can opt for a abstract class , if not then you can go for interface
Venkat K replied to myzonal.com myzonal.com on 13-Jun-12 07:05 AM
It's Simple! You are forcing user to implement the default logic using the abstract class. where as in interface you will just define only the functions but not any logic.

Generic Example:
Suppose you want to construct a object CAR. Then you can implement the default logic that your car needs to have 4 wheels in an abstract class.

You can search in .net for more realtime examples.

Regards
Venkat
Jitendra Faye replied to myzonal.com myzonal.com on 13-Jun-12 07:41 AM
Interface has only rules not implementation,

but abstract class can have rules with implementation.

It is depends on your requirement.
DL M replied to myzonal.com myzonal.com on 13-Jun-12 07:43 AM
Hi..

Abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).
dipa ahuja replied to myzonal.com myzonal.com on 13-Jun-12 07:45 AM
(1)  An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface cannot implement methods.
 
(2)  An abstract class can contain fields, constructors, or destructors and implement properties. An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
 
(3)  An abstract class cannot support multiple inheritances, but an interface can support multiple inheritances. Thus a class may inherit several interfaces but only one abstract class.
 
(4)  A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
 
(5)  Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
 
(6) Abstract classes are are faster than interfaces.