#1,163 – An Abstract Event Has No Implementation

An abstract event is a special kind of virtual event, defined in a base class, which has no implementation of its own.  A derived class must define every abstract event (as well as every other abstract member) using the override keyword.

    public abstract class Dog 
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
        }

        public abstract event EventHandler<string> Barked;

        public virtual void Bark()
        {
            Console.WriteLine("Woof");
        }
    }

    public class Terrier : Dog
    {
        public Terrier(string name) : base(name) { }

        public override event EventHandler<string> Barked;

        public override void Bark()
        {
            Console.WriteLine("Yip yip");
            Barked(this, "Yip yip");
        }
    }

The class in which the abstract event is defined must be defined as an abstract class, using the abstract keyword.

A derived class may not hide the abstract event in the base class, using the new keyword.  It must implement the inherited abstract event and the new event must be virtual, defined using the override keyword.

Example of using this event:

            Terrier t = new Terrier("Jack");
            t.Barked += (o, s) => Console.WriteLine("- " + s + " -");
            t.Bark();

1163-001

Advertisement

#361 – An Abstract Property Has No Implementation

You can declare a property as abstract, indicating that it has no implementation in the base class.  The base class indicates the property’s type and whether it has get and set accessors in any derived classes.

A base class that defines an abstract property must also be marked as abstract itself.  It wouldn’t make sense to create instances of the base class, since the implementation for the abstract property is missing.

A derived class must provide the implementation (in get/set accessors) for any properties that are abstract in the base class.

Defining an abstract property:

    public abstract class Dog
    {
        public abstract string Temperament { get; }
    }

Providing an implementation for the get accessor in a derived class:

    public class Terrier : Dog
    {
        protected string temperament;
        public override string Temperament
        {
            get
            {
                return string.Format("Terrier {0} is {1}", Name, temperament);
            }
        }

#353 – Why You Might Define an Abstract Class

An abstract class cannot be instantiated and is meant to serve as a base class for other classes.  It might contain implementations for its methods, which derived classes would inherit.  It might also contain one or more abstract methods, with no implementation, which must be overridden in the derived classes.

You’d typically use an abstract class when it would only make sense to instantiate the derived classes and when the base class just serves as a blueprint for the derived classes and to possibly include the implementation of some methods.

For example, you might have a Person class and Man and Woman classes that inherit from Person.  You can imagine only ever creating instances of Man and Woman and never of a generic Person–everyone is either a man or a woman.  You can imagine methods implemented in Person that are common to both men and women, like Sing.

#352 – You Can’t Instantiate an Abstract Class

An abstract class, defined with the abstract keyword, is one that is not meant to be instantiated itself, but serve as a base class for other classes.  A class must be abstract if it contains any abstract methods.

Since an abstract method does not contain an implementation, it doesn’t make sense to instantiate the class containing the abstract method.

You can, however, have non-abstract methods in an abstract class.  Derived classes will inherit the implementation of these methods and could then make use of them.

#351 – An Abstract Method Has No Implementation

An abstract method is a special kind of virtual method, defined in a base class, which has no implementation of its own.  A derived class must define every abstract method and provide an implementation, using the override keyword.

    public abstract class Dog
    {
        // Abstract method has no implementation
        public abstract void Bark();
    }

    public class Terrier : Dog
    {
        public override void Bark()
        {
            Console.WriteLine("Terrier {0} is barking", Name);
        }
    }

The class in which the abstract method is defined must be defined as an abstract class, using the abstract keyword.

A derived class may not hide the abstract method in the base class, using the new keyword.  It must implement the inherited abstract method and the new method must be a virtual method, defined using the override keyword.