tags:

views:

119

answers:

5

Hello All, I like to know what makes a class to be called as absract class. I believe, abract key word definetly make a class class, but if one takes out the keyword, then we can create the instance of the class.

In otherwords, what are the characteristics of the abstract class.

Thanks in advance.

-Harsha

+2  A: 

An abstract class has one singular characteristic: no instances of the class can be created. Abstract classes are designed to be used as base classes for other abstract and non-abstract classes.

Andrew Hare
+1  A: 

For the abstract on the class to have any meaning, it must have abstract members that need implementing, otherwise it's extraneous (unless you intended to prevent instantiation) and you should remove it.

It might have an abstract property an inheritor needs to declare, like this

public abstract int ID { get; }

or a method it must implement:

public abstract void DoSomething();

You would use it if the class itself was never to be instantiated but you needed more than an interface, for example base properties and you wanted to use it later:

((MyAbstractClass)obj).DoSomething(); //All inheritors implemented this
Nick Craver
+4  A: 

An abstract class does not form a concrete object in the real world unlike pure implementation classes. Abstract as the name suggestes they hold/define common behaviours of related objects that need to be reused/defined independantly in all related objects.

Take an example of Birds. If you are writing a progrm that will have something to do with the birds, then you'll first have an abstract base class as Bird and each bird deriving from the abstract base class Bird. Do note that abstract class BIRD does not represent a concrete real world object but a type of related objects that is birds!

Lets start with the class-diagram and then some code.

alt text

public abstract class Bird
{
    protected string Name = string.Empty;
    public Bird(string name)
    {
        this.Name = name;
    }

    public virtual void Fly()
    {
        Console.WriteLine(string.Format("{0} is flying.", this.Name));
    }

    public virtual void Run()
    {
        Console.WriteLine(string.Format("{0} cannot run.", this.Name));
    }
}

public class Parrot : Bird
{
    public Parrot() : base("parrot") { }
}

public class Sparrow : Bird
{
    public Sparrow() : base("sparrow") { }
}

public class Penguin : Bird
{
    public Penguin() : base("penguin") { }

    public override void Fly()
    {
        Console.WriteLine(string.Format("{0} cannot fly. Some birds do not fly.", this.Name));
    }

    public override void Run()
    {
        Console.WriteLine(string.Format("{0} is running. Some birds do run.", this.Name));
    }
}

class Program
{
    static void Main(string[] args)
    {

        Parrot p = new Parrot();
        Sparrow s = new Sparrow();
        Penguin pe = new Penguin();

        List<Bird> birds = new List<Bird>();

        birds.Add(p);
        birds.Add(s);
        birds.Add(pe);

        foreach (Bird bird in birds)
        {
            bird.Fly();
            bird.Run();
        }

        Console.ReadLine();
    }
}
this. __curious_geek
-1 for using animal examples.
Lotus Notes
did ı hurt any sentiments? can u please justify the downvote ?
this. __curious_geek
Thank you very much for your time and for the reply. Is any other unique characters you could remember. Thanks again
Harsha
+1  A: 

An abstract class serves two related purposes:

  1. You can place abstract members into the class
  2. You cannot create an instance of the abstract class

The main purpose of an abstract class is to serve as a base class for descendant classes, providing perhaps some common functionality, but at least providing a common set of methods that the descendants has to implement.

For instance, you could create an abstract class Fruit, with an abstract method Eat. Since you cannot actually eat a "fruit", you need to eat a specific type of fruit, the class is abstract and you will never see an object that is labelled "Fruit" in the wild.

Instead, you will see objects of type Apple, Banana, Orange, etc. that are descendants of Fruit, and which have an actual implementation of Eat.

Other than the two things at the top, there is nothing special about an abstract class.

Lasse V. Karlsen
+1  A: 

I don't think anybody has said this yet, but I believe the distinguishing characteristic of an abstract class is:

The coder knows how X% of the class is implemented, and how (100-X)% of the class should be implemented, but which cannot be.

This is what distinguishes the abstract class. Interfaces describe how a class should be implemented, but do not give implementors any code or support. Base classes with virtual methods provide default implementations that inheritors may possibly override.

It is only the abstract class that can provide a partial implementation, but cannot provide a complete implementation.

Will