Tuesday, March 15, 2016

Lesson 9

Outline

Chapter 12

Abstract Class

A C# abstract class contains abstract members which define what a derived should contain. These abstract members only declare that a member of a particular type is required, it does not implement the member. Implementation of abstract members takes place within the derived class. If a subclass which derives from an abstract class and fails to implement abstract methods will fail to compile.

In an abstract class, the abstract methods and abstract procedures are described but not defined.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
    }

Note the addition of the keyword "abstract" in describing the class, property and method. It will be the responsibility of a derived class to implement the functionality described in the abstracted class.

    class ImplementedSample : Sample
    {
        public override int MyProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override void DoSomething()
        {
            throw new NotImplementedException();
        }
    }

When inheriting from an abstract class the concrete class must implement the design of the abstract class. A wizard can appear over the abstracted class name and stub out the abstraction requirements for us. Visual studio adds the NotImplementedException to remind us that the method or property code has not been defined.

An abstracted class can implement functionality as well that can be derived.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
        public void DoSomethingElse()
        {
            //This is where I would put code
        }
    }

The DoSomethingElse() method has code and is functional. Any derived class can call this method. The method functionality cannot be changed in the derived class.

Virtual

So far we have only looked at abstract class members. As discussed above an abstract member is not implemented in the base class and must be implemented in derived classes in order for the class to compile.
Another type of member is a virtual member. A member defined as virtual must be implemented in the base class, but may be optionally overridden in the derived class if different behavior is required.

Lets add a virtual method to the abstract class.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
        public void DoSomethingElse()
        {
            //This is where I would put code
        }
        public virtual void StillDoSomethingElse()
        {
            //This code can the overridden and changed in the derived class.
            //If not overridden, then this code will run when the method is called.
        }
    }

Introducing the virtual keyword allows us to change the behavior of the method downstream in a derived class.

Lets look at the implementation.

    class ImplementedSample : Sample
    {
        public override int MyProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override void DoSomething()
        {
            throw new NotImplementedException();
        }
        public override void StillDoSomethingElse()
        {
            //By using the override keyword on this method
            //the code here will run instead of the base abstract method code
        }
    }

By allowing us to change the behavior of the method through overriding each derived class can use its own implementation of the virtual method or default to the method as defined.

Implementing abstract classes allow us to define behavior in the derived classes and implement behavior change when necessary.

No comments:

Post a Comment