difference between an abstract class and interface

Posted by Saranya Boopathi under ASP.NET on 6/11/2013 | Points: 10 | Views : 3340 | Status : [Member] | Replies : 5
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: 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