C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Videos


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...


Birthday Greetings
Learn Windows 7: Shortcut keys of bolt browser   Bolt mobile browser is a easy to use or very useful web browser.it supports.it supports all of the Java enabled phone.Download it and enjoy unlimited exoeriance



Forums » .NET » .NET »

Difference between Abstract Class and Interface


Posted Date: 16 Mar 2005      Posted By:: Paramdeep Jain    Member Level: Bronze    Member Rank: 0     Points: 2   Responses: 8



Hi,
Have came across this few times from people who take the DotNet Interview. Please can some one explain in simple terms in details what is the Difference between Abstract Class and Interface and why to use one over other while designing.

Thanks
Paramdeep





Responses

Author: Nitin Goyal     Member Level: Bronze      Member Rank: 0     Date: 16/Mar/2005   Rating: 2 out of 52 out of 5     Points: 2

The basic difference between an Abstract class and an interface is that Abstract class allows Concrete method(methods that have implementation) but interface doesn't. The second difference is in terms of inheritance. A class can inherit from only one class but can implement any number of interfaces. One more difference is that interface doesn't allow variables/constants to be declared but abstract class allows for declaration of variables and constants.

You should consider using interfaces when your design is complete in itself. i.e., no further methods need to be added later on to the interface as any newly added method in interface need to be implemented in all the classes that implement that interface. On the other hand if we need to add a new method to an abstract class, we can provide default implementation of that method in abstract class and there is no need for existing implementing classes to be changed in that case.



Author: Brainstorming Guy     Member Level: Diamond      Member Rank: 0     Date: 16/Mar/2005   Rating: 2 out of 52 out of 5     Points: 2

Hai,
This is straight from MSDN.
The choice of whether to design your functionality as an interface or an abstract class (a MustInherit class in Visual Basic) can sometimes be a difficult one. An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes. For details, see Abstract Classes.

An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.

Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. For example:

' Visual Basic
Public Sub Spin (ByVal widget As IWidget)
End Sub
// C#
public void Spin (IWidget widget)
{}
This method could accept any object that implemented IWidget as the widget argument, even though the implementations of IWidget might be quite different. Abstract classes also allow for this kind of polymorphism, but with a few caveats:

Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.
Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Hope it helps.
If you have any queries, please feel free to post here.
Happy Coding!!!

Regards,
Brainstorming Guys

Regards,
Brainstorming Guy aka Venkatarajan A

BEEN THERE. DESTROYED THAT.



Author: Sandip Naskar     Member Level: Gold      Member Rank: 0     Date: 17/Mar/2005   Rating: 2 out of 52 out of 5     Points: 2

Hi,

You can visit the url below,
http://www.dotnetspider.com/technology/kbpages/1041.aspx

Thank you,
Sandip



Author: DCPL - Ram     Member Level: Diamond      Member Rank: 20     Date: 14/Aug/2008   Rating: 2 out of 52 out of 5     Points: 6

Abstract Class..
1.It cannot defines all the methods
2.It has subclass.
3.Here, Subclass is useless
4.A class can be extend an abstract class

Interface..
1.It defines all the methods
2.It must have implementations by other classes, But there will be no use of that.
3.Only an interface can extend another interface.

For More details..
http://kyapoocha.com/c-sharp-interview-questions/what%E2%80%99s-the-difference-between-an-interface-and-abstract-class-5/
http://www.dotnetuncle.com/Difference/4_abstract_class_interface.aspx
http://forums.msdn.microsoft.com/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd/

Regards
Sridhar R
Nothing is illegal, Until You Get Caught
With Tears...Sridhar R



Author: Bunty       Member Level: Diamond      Member Rank: 15     Date: 16/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

Hi,


Interfaces are similar to abstract classes.However,interface represent the highest level of abstraction in object-oriented programming.This is because all the methods in an interface are abstract and do not have implementation.In contrast,the abstract classes that are created using Abstract keyword might contain a method that has a body.

Regards
S.S.Bajoria


Thanks & Regards


Bunty



Author: Bunty       Member Level: Diamond      Member Rank: 15     Date: 22/Sep/2008   Rating: 2 out of 52 out of 5     Points: 6

Hi,

Following are the difference between abstract and interface,

1>Abstract class having method declaration as well as method method definition whereas interface having method declaration only.

2>Abstract class are known as partial abstract class whereas interface is known as fully abstract class.

3>Abstract class features we have to inherit to the child class whereas interface features we have to implement in the child classes.

4>Abstract class support access specifiers whereas interface doesn't support access specifiers.

5>Abstract class have normal variable as well as constant variable whereas interface have only constant variables.

6>We can write constructor in abstract class whereas we can't write constructor in interface.

Regards
S.S.Bajoria


Thanks & Regards


Bunty



Author: Miss Meetu Choudhary       Member Level: Diamond      Member Rank: 21     Date: 16/Oct/2008   Rating: 2 out of 52 out of 5     Points: 6

Following are the differences

• Interface contains methods that must be abstract; abstract class may contain concrete methods.
• Interface contains variables that must be static and final; abstract class may contain non-final and final variables.
• Interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
• Interface is absolutely abstract; abstract class can be invoked if a main() exists.
• Interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.
• Members in an interface are public by default; abstract class may contain non-public members.
• Interface is used to "implements"; whereas abstract class is used to "extends".
• Interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.

==
Thanks and Regards
Meetu Choudhary

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green
http://www.jaipurmentor.com/
My Profile on Google



Author: Miss Meetu Choudhary       Member Level: Diamond      Member Rank: 21     Date: 21/Feb/2009   Rating: 2 out of 52 out of 5     Points: 6

Abstract Class:



The class which contains the common features of components of several classes, but cannot it be instantiated by itself. It represents an abstract concept for which there is no actual existing expression. For instance, "Vegetation" is an abstract class - there is no such real, real thing as generic vegetation. Instead, there are only instances of vegetation, such as mango tree and rose plant, which are types of vegetation, and share common characteristics, such as having leaves and stem in at least part of the lifecycle.



SO in software engineering, an abstract class is a class in a nominative type system which is declared by the programmer, and which has the property that it contains members which are also members of some declared subtype. In many object oriented programming languages, abstract classes are known as abstract base classes, interfaces, traits, mixins, flavors, or roles. Note that these names refer to different language constructs which are (or may be) used to implement abstract types.





We can also say that abstract class is : -- A class which is used only as an ancestor and is never instantiated.



In other word a concrete definition will say that



A type of class with pure virtual member functions and one or more methods that are declared but not implemented, that behaves as a base class but prohibits the instantiation of any members of that class. i.e. It has a complete interface but only a partial implementation It is used to take advantage of inheritance yet prohibiting the generation of objects that are not completely defined. Concrete subclasses of an abstract class are required to flesh out the implementation by overriding the abstract methods.

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green
http://www.jaipurmentor.com/
My Profile on Google



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Previous : Is there any expert to solve this?
Return to Discussion Forum
Post New Message
Category: .NET



About Us    Contact Us    Privacy Policy    Terms Of Use