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
|
| Author: Bharat 04 Oct 2005 | Member 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 2006 | Member 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 2006 | Member 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 2006 | Member Level: Bronze Points : 0 |
http://objectspot.blogspot.com/
|
| Author: Miss Meetu Choudhary 21 Feb 2009 | Member 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.
|