Being Abstract: Abstract Classes

 

To begin thinking about Object Oriented Programming, let’s start with the definition of a class.

A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types.

Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class. 

I think a good place to start is with the Abstract Classes, if you are going to build Windows Phone 7 software for Microsoft Marketplace, you will want to save time by reusing code.  This is important for games and cloud applications.  Keep in mind that Microsoft code works easily on many platforms, Client, Server, Phone, MicroFrameworks, Compact Framework and XBox 360.

In this example, and the example will compile, but doesn’t do anything.  In the example, an abstract class Avatar creates an abstract method RatFink.

  • Class BeepBeepCreep implements an additional method GoofyPoofy. BeepBeepCreep must also be declared abstract since it doesn’t provide an implementation of RatFink.
  • Class CatBat overrides RatFink and provides an actual implementation.
  • Since there are no abstract members in CatBat, CatBat is permitted (but not required) to be non-abstract.

Abstract Example: ClassDiagram1

using System;
using System.Collections.Generic;

namespace Avatar_Class
{
    abstract class Avatar
    {
        public abstract void RatFink();
    }

    abstract class BeepBeepCreep : Avatar
    {
        public void GoofyPoofy() { }
    }

    class CatBat : BeepBeepCreep
    {
        public override void RatFink()
        {
            // actual implementation of F
        }
    }

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

        }
    }
}

 

 

 

In conclusion:

You use abstract classes to create other classes and save time by reusing code 

  • Abstract classes can only be used as base classes
  • Abstract can only be derived and Sealed classes cannot be derived