I got the following question in a Yahoo group:
I read few articles on Abstract Classes but it is still not clear to
me. What exactly an "Abstract" keyword means? In which situation to use
Abstract Class?
In response to that question I wrote the following article and thought to share it.
Abstract class:
It is a class which can not be initialized. You should make a class an abstract class when there is atleast one abstract method in your class. Abstract class can have abstract as well as non abstract method. Abstract class can not be used directly, instead you need to derive class from the abstact class.
Difference between abstract class and an interface is that:
Abstract class can have internal member, while interface can not. Abstract class can have abstract and non-abstract method while intreface contains only the signature.
When to use abstract class:
Make a class an abstract class when there is atleast one abstract method in your class. When you want your class to be act as a base class and you dont want that object of your class could be created.
Example:
Let’s consider a very
simple and basic example. We need to write two classes for
From the name of these two
classes you can imagine that what these two classes are.
Common things between
these two types of class are:
- Both types of classes have
same number of Legs.
- Both are living creatures.
The uncommon things b/w these
two is that Lion eats meats while goat does not.
Wrong Solution:
If you are asked to write
classes to handle these two classes then you can do the following things:
Write two classes (i.e. Lion and Goat) with the following function along with their Implementation:
public void NoOfLegs()
public void IsLivingCreature()
public void DoesEatMeat()
You have achieved your
solution, but it is not a good solution. Now lets write code to achieve the
same thing in a different way.
A better solution:
Write a class "Common" like this:
public abstract class common
{
public void NoOfLegs()
{
Console.WriteLine("No. of legs are: 4");
}
public void IsLivingCreature()
{
Console.WriteLine("Yes");
}
public abstract void DoesEatMeat();
} |
You can see that we have implemented those function which are common in Lion and Goat class, so that we can avoid to write code for these two function again in Lion and Goat class, because we will derive our Lion and Goat classes from this common class. We made the DoesEatMeat() an abstract method because Lion and Goat's behavior are differnet for this (i.e. Lion eat meat while goat does not). Here is the Lion and Goat class:
public class Lion : common
{
public override void DoesEatMeat()
{
Console.WriteLine("Yes");
}
}
public class Goat : common
{
public override void DoesEatMeat()
{
Console.WriteLine("No");
}
}
|
In the above code you can see that we derived two classes Lion and Goat from the common class and override only the DoesEatMeat according to the behavior of Lion and Goat.
We can not achieve the same thing using interface, because there is no way in interface to provide the implementation of common behaviors between two entities. So from this it should be clear that when to use interface and when to use abstract class.
The complete sample code with along with the out put is given below:
|
using System;
using System.Collections.Generic;
using System.Text;
namespace WhenAbstractClass
{
class Program
{
static void Main(string[] args)
{
Lion lion = new Lion();
lion.NoOfLegs();
lion.IsLivingCreature();
lion.DoesEatMeat();
Goat goat = new Goat();
goat.NoOfLegs();
goat.IsLivingCreature();
goat.DoesEatMeat();
}
}
public abstract class common
{
public void NoOfLegs()
{
Console.WriteLine("No. of legs are: 4");
}
public void IsLivingCreature()
{
Console.WriteLine("Yes");
}
public abstract void DoesEatMeat();
}
public class Lion : common
{
public override void DoesEatMeat()
{
Console.WriteLine("Yes");
}
}
public class Goat : common
{
public override void DoesEatMeat()
{
Console.WriteLine("No");
}
}
}
|
Out Put:
Copy and pase the above code in a c# console based application :)