|
| Difference between Abstract class and interface |
 |
Mon, 31 Mar 2008 10:22:56 +000 |
Difference between Abstract class and interface-
1. The abstract class allows concrete methods (methods that have
implementation) but interface doesn’t not.
2. A class can inherit from only one class, but can implement any number
of interfaces.
3. Interface doesn’t allow variables/constants to be declared but
abstract class allows for declaration of variables and constants.
4. You should consider using interface when you design is complete in
itself i.e. no further methods need to be added later on the interface as any
newly added method in interface need to be implemented in all 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 the
abstract class and there is no need for existing implementing classes to be
changed in that case.
But, still a question needs to address “how we decide where to implement
interface and where we need to use interface”.
As word “interface” meaning itself describe contract between 2 classes where
derived class need to implement all methods of interface. We need to implement
interface where there is no inheritance properties between classes. We need
declare interface when we need to force user to implement certain methods.
Let’s take an example.
publicclassTest : IDisposable
{
public Test()
{
//
// TODO: Add constructor logic here
//
}
publicvoid Dispose()
{
//throw new Exception("The method or operation is not
implemented.");
}
}
In above example, there is not generic similarity between Test class and
IDisposable interface. It is just a contract between 2 classes and user has been
forced to implement Dispose method.
On the other side, there is generic similarity between two classes in case of
abstract class. Let’s take an example.
abstractclassPerson
{
int m_personID;
string m_firstName;
string m_lastName;
string m_emailAddess;
constdouble minSalary = 3000;
publicdouble returnSalary()
{
return minSalary;
}
publicint PersonID
{
get { return m_personID; }
set { m_personID = value; }
}
publicstring FirstName
{
get { return m_firstName; }
set { m_firstName = value; }
}
publicstring LastName
{
get { return m_firstName; }
set { m_lastName = value; }
}
publicstring EmailAddess
{
get { return m_emailAddess; }
set { m_emailAddess = value; }
}
}
publicclassEmployee : Person
{
}
In the above example, we can see that there are properties of base have been
derived by child class. There is generic similarity between Person and Employee
class.
|
| Post Reply
|
| Re: Difference between Abstract class and interface |
 |
Mon, 31 Mar 2008 13:52:58 +000 |
I just read two of your other tips today, very nice. They are going into my
favorites section, for when I work with/teach new team members.
satya_chhikara:
Difference between Abstract class and interface-
1. Interface doesn’t allow variables/constants to be declared but
abstract class allows for declaration of variables and constants.
4.
I thought this clarification might be useful. Interfaces do allow you to define
properties, just not fields/constants.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|