[Last Call] Learn how to a build a cloud-first strategyRegister Now


What is importance of Abstract Classes

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

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

Accepted Solution

PoeticAudio earned 2000 total points
ID: 16828298
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)

Expert Comment

ID: 16828340
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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 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