What is importance of Abstract Classes

Posted on 2006-06-04
Last Modified: 2010-08-05
Hello Experts

What is importace of Abstract Classes
What are adventages to use abstract classes
Question by:MuhammadAdil
    LVL 6

    Accepted Solution

    Abstract classes allow you to abstract out common functionality that all of your derrived classes have in common. You can have methods in your abstract class that are either marked as abstract, which means they don't have implementation in the abstract class but the derrived classes are forced to override and provide their own implementation or marked as virtual, which means the abstract class can provide some basic implementation but the derrived classes can override this implementation and provide their own (and also use the abstract classes implementation as well). Also you can have your standard methods in abstract classes that will apply to all derrived classes.

    There are many advantages to abstract classes, some of them include:

    Creates an object oriented architecture helping provde real world objects.

    Allows you to dynamically call methods in derrived classes, even if you don't know what derrived class your currently working with. For example you could have an abstract class Animal and derrived classes Dog and Bird. Because they both derrive from animal if you run them through a method, say "MakeAnimalSpeak" you don't have to know which animal it is, as long as it derrives from abstract class Animal it will know how to speakl

    Provides more opportunity for code-reuse. Because abstract classes provide some base functionality, there are better chances that you can use this class in other projects and in other situations, this will make it so you write less code in the future.

    Here are some examples of the above mentioned.

    We can use abstract methods that force the derrived classes to provide implementation.

    Here is a very basic demonstration of this.

    abstract class Animal
        public abstract void Speak();

    class Dog : Animal
        public override void Speak()

    class Bird : Animal
        public override void Speak()

    Notice in Anmial the Speak method is marked as abstract because we don't need any base implementation here, we just want derrived classes to implement a speak method.

    What's the point of this? Well it forces your derrived classes to implement a speak method, so now at run-time we know that if it's an Animal, then we can make it speak. Here is what I mean. Lets have the user type in a textbox what animal they want to hear speak, that way this is more dynamic (there is no error handling code because I want to illustrate this point)

    Notice a couple things here. We create an animal dynamically, we don't know at design time which animal we are working with, we just know that we are working with an animal. Also, notice that we are just passing an Animal to the MakeAnimalSpeak method, again we don't know which animal is being passed through, we just know that it can speak. I named the TextBox txtAnimalType, and just left the button default as Button1. Focus on the code for a few minutes and it should come together.

    private void button1_Click(object sender, EventArgs e)
        //We just create an animal, don't care which one and call speak on it
        Animal animal = CreateAnimal(txtAnimaType.Text.Trim());

    private Animal CreateAnimal(string animalType)
         //lets create an animal object based on a string
         //in this example we are passing the string of a txtAnimalType through
         //to create and return an animal that corresponds with the string
         switch (animalType.ToUpper())
              case "DOG":     return new Dog();      
              case "BIRD":    return new Bird();
              default:        return null;

    private void MakeAnimalSpeak(Animal animal)
        //if it's null then we can't make it speak, so we'll prompt the user of the error
        if (animal == null)
             MessageBox.Show("Unknown animal!");

    That's one use for abstract classes. This is a very basic example (which uses what's called a factory method to create the animal). The other big bonus with abstract classes is providing some base implementation in the abstract class and either using the base implementation or overriding it (if it's virtual) and providing more implementation in the derrived class as well as using the base class implementation, great for code-reuse.

    If you want examples of this then please let me know, i'll be more then happy to help (I just ran out of time or I would do it now)
    LVL 6

    Expert Comment

    Also notice that you can create whatever methods in your derrived classes... like you could make Dog have a fetch method, it will still run through the MakeAnimalSpeak method just fine, but you cant make a MakeAnimalFetch method because the bird doesn't have a fetch method... you could make a MakeDogFetch method and have classes derrive from Dog... like Chow, Doberman...etc run through the method fine as long as you mark the Fetch method as virtual.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now