difference between an abstract class and interface

Posted by Saranya Boopathi under ASP.NET on 6/11/2013 | Points: 10 | Views : 2255 | Status : [Member] | Replies : 6
Whats the difference between an abstract class and interface? When would you want to use them?

Saranya Boopathi


Responses

Posted by: Niladri.Biswas on: 6/11/2013 [Member] Platinum | Points: 25

Up
0
Down
Abstract classes can include implementations of methods which can be over-ridden in derived classes. Interfaces cannot include implementations.

basically, abstract class can and its widelly used to create Base classes, where you can define (abstract) methods, with no implementation!!, forcing the implemntation to be explicit on all the classes that derive from that base class.

interfaces. allow you to define signatures for methods that must be implemented in a class that implements that interface. (hope im not being to confusing!). this is good for IoC where you can pass interfaces as parameters not caring at all with its implementation. You also use interfaces to expose a restricted set of methods when creating a Webservice (asmx or wcf)

http://forums.asp.net/t/1760762.aspx/1

Best Regards,
Niladri Biswas

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 6/11/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello

The most important thing for an abstract class that is its object cannot be created.It is used in inherited form

First Interface cannot be called as class,It has no implementation,it has method without the body


For complete info please check this link
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 6/11/2013 [Member] Starter | Points: 25

Up
0
Down
hi Saranya,
Abstract Class:
Classes containing at least one abstract method are called abstract classes, and must include the abstract keyword before the class keyword. If you forget the abstract keyword when defining the class, you get an error from the C# compiler:

Abstract classes use the abstract keyword. They do not have a method body; a semicolon follows the parameter list instead.

Abstract methods are also, by definition, virtual methods, and must be overridden by derived classes using the override keyword:

The C# compiler doesn't allow you to create objects from abstract classes. If you try to create an object from an abstract class

Interface:
Interfaces describe a group of related functionalities that can belong to any classor struct.

Interfaces can consist of methods, properties, events, indexers, or any combination of those four member type

Interfaces members are automatically public.
When a class or struct is said to inherit an interface, it means that the class or struct provides an implementation for all of the members defined by the interface.

A class or struct can inherit more than one interface.

When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.
I hope it will help you
Mark as answer if it helps you
Thanks&Regards


Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 6/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Abstract Class:- 


1) An abstract method is created by specifying the abstract type modifier.
2) An abstract method contains no body.
3) An abstract method is not implemented by the base class.
4) An abstract method is automatically virtual.
5) A derived class must override it.
6) Abstract class can have modifiers for methods,properties etc.,
7) An abstract class can implement a property.
8) The abstract modifier cannot be applied to static methods.
9) Properties can also be abstract.
10) A class containing abstract methods must be declared as abstract with the abstract specifier.
11) There can be no objects of an abstract class.
12) 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.
13) An abstract class can inherit from a class and one or more interfaces.
14) An abstract class can implement code with non-Abstract methods.
15) Abstract class can have constant and fields.
16) An abstract class can have constructors or destructor's.
17) An abstract class cannot be inherited from by structures.
18) An abstract class cannot support multiple inheritance.
19) 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:-

1) Interfaces cannot be instantiated directly.
2) Interfaces can contain events, method, indexer and properties.
3) An Interface can contain property definitions.
4) Interfaces contain no implementation of methods.
5) Classes and Structs can implement more than one interface.
6) An Interface can be inherited from by structures.
7) An interface itself can inherit from multiple interfaces (Interface can support multiple
inheritance).
8) An abstract class can implement a property.
9) 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.


If this post helps you mark it as answer
Thanks

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 6/12/2013 [Member] Starter | Points: 25

Up
0
Down
Abstract Class
1.It can contain Concrete methods(methods with implementation) So in other words, Abstract class can contain methods with both implemetaion
and without implementation
2.Multiple inheritance is not possible in case of abstract class
3.Access Specifiers are been Supported in abstract class

Interface
1.Does not contain any concrete methods
2.Multiple Inheritance is possible with interface
3.Access Specifiers are not supported in Interface

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 6/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below link to clear your concept:-
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
http://www.aspdotnet-suresh.com/2010/04/abstract-versus-interface.html
Hope this will help you.

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response