C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Abstract Class Vs Interface


Posted Date: 27 Sep 2005    Resource Type: Articles    Category: .NET Framework
Author: Nabin Kumar PadhiMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction



What is an Abstract class?



An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.



An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Both together


When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is the same as an interface but with the restriction that it cannot make a class inherit from it. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:Look at in Summary

Summary


Click 'Comparison Table' to view the Table




Responses

Author: Bharat    04 Oct 2005Member Level: Bronze   Points : 0
In this you have use a word "C# doesnt support multiple inheritence" instead of this u have to say that c# dosent support mulitcode inheritence now u say what is the difference
look
inheritence meance creating new object with the help of old one
when we create a new object with the helpof more then one object(class or interface) it is known as multipal inheritence
show
if i say
class a inherits b implement c
how many classes a or interface a is inheriting
more then 1
so isnt it mulitple inheritence
yes it is but it is multipal code inheritence
please forward me u r view waiting for u r mail


Author: Nabin Kumar Padhi    19 Feb 2006Member Level: Bronze   Points : 0
"Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance."

You have mistaken the word " Multiple Inheritance"
inherit from Multiple class and implements the multiple Interface and completly different, we are not able to inherit from multiple class in c# that is the reason we are implementing the Interface to achived the Multiple Inheritance.

If you are talking abt something different then i m sorry , i would like to know....

Thanx
Regards
Nabin Padhi



Author: Nabin Kumar Padhi    19 Feb 2006Member Level: Bronze   Points : 0
"Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance."

Sorry for delayed in review....

You have mistaken the word " Multiple Inheritance"
Inherit from Multiple class and implements the multiple Interface are completly different, we are not able to inherit from multiple class in c# that is the reason we are implementing the Interface to achived the Multiple Inheritance.

If you are talking abt something different then i m sorry , i would like to know....

Thanx
Regards
Nabin Padhi



Author: Mahesh    05 Jul 2006Member Level: Bronze   Points : 0
http://objectspot.blogspot.com/


Author: Miss Meetu Choudhary    21 Feb 2009Member Level: Diamond   Points : 2
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.






Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Interface  .  Difference  .  Abstract Class  .  .Net Framework  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Using Stack in C#
Previous Resource: Spell Check with RichTextBox Control
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use