Latest

random

Abstract Class C# Tutorial for Beginners

C# Tutorial - Understanding C# Abstract Classes


In my previous article I explained about C# Interface Tutorial for Beginners. In this post I will explain about the abstract class, abstract method and abstract properties.

What is Abstract classes in C#?

An abstract class is a conceptual class that can not be instantiated, but can make derivations of this. Or

Abstract classes are used when we want to share common functionality in parent child relationship. Find the example below:

C# Tutorial - Abstract classes in c#, All about abstract classes,Understanding C# Abstract Classes,C# tutorial for beginners, in csharp with example, Abstract class in c# example

All about abstract classes

  • An abstract class can contain either abstract methods or non abstract methods
  • You can't declare an abstract constructor in a abstract class but you can have a constructor on your abstract class.
  • Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class. 
  • It is mandatory to override abstract method in the derived class.
  • Virtual or abstract member cannot be private. 
  • An abstract class cannot be a sealed class
  • We can create abstract properties with in a abstract class.
  • An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
  • An abstract member cannot be static.
  • All derived classes must implement all the abstract methods in it.
Abstract Class in C# Example

namespace abstractclass
{
   abstract class absClass
    {
        public abstract void absMethod();       
        public void absNonAbstractMethod()
        {
            Console.WriteLine("abs non abstract method");
        }

        protected int myNumber;
        public abstract int numbers
        {
            get;
            set;
        }
    }
}

------------------------------------------------------------------------------------
namespace abstractclass
{
    class derivedClass:absClass
    {
        public override void absMethod()
        {
            Console.WriteLine("abs method");
        }

        public override int numbers
        {
            get
            {
                return myNumber;
            }
            set
            {
                myNumber = value;
            }
        }
    }
   
}

-------------------------------------------------------------------------------------
namespace abstractclass
{
    class Program
    {       
        static void Main(string[] args)
        {
            derivedClass obj = new derivedClass();
            obj.absNonAbstractMethod();
            Console.WriteLine();
            obj.absMethod();
            obj.numbers = 56;
            int b = obj.numbers;
            Console.Write(b);
            Console.ReadKey();
        }
    }
}
Download the source code click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading .. :)

No comments:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.