Abstract Class and Abstract Methods

In this article I will explain about abstract class and abstract methods.

 

What are Abstract Class and Abstract Methods?

 

Definition of Abstract Class

 

Also called an "abstract super class," in object technology, it is a class created as a master structure. No objects of an abstract class are created, rather subclasses of the abstract class are defined with their own variations, and the subclasses are used to create the actual objects.

 

Something about Class:

 

Class is an abstract type

Class is a template

Class is a user-defined type.

Class is an abstract type.

 

Abstract class

 

Abstract is incomplete or can say partial information.

 

We can't directly initiate abstract class because it is incomplete (It doesn't have complete functionality).

 

It is initiated through derived class when complete functionality is done.

 

Abstract class has partial information, which is fully done in derived class.

 

Abstract class may have abstract methods or complete methods.

 

Abstract class can have a constructor, which is used to initialize data members of the abstract class, which will be initiated indirectly with help of derived class.

 

If an abstract class has a private constructor we have to create an inner class to initialize abstract class.

 

Abstract classes have the following features

  • Abstract class can't be static.
  • Abstract class is used for inheritance.
  • Abstract class can't be sealed.
  • Abstract or virtual members can't be private, as we can't override it.
  • Abstract class can be inherited from an abstract class but the methods in the base class have to be declared abstract.

 Abstract methods have the following features 

  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes.
  • The implementation is provided by an overriding method, which is a member of a non-abstract class.
  • It is an error to use the static or virtual modifiers in an abstract method declaration.
  • An abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon e.g.: public abstract void Display();

Note: In inheritance we can't change the access modifier of a member method in lower level because it has to check for method declaration (which method to override i.e. signature identification).

 

Practical demonstration of abstract class:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace abstract_class

{

    class Program

    {

        public abstract class XX

        {

            public abstract int sum(int a, int b);

            public abstract int subt(int a, int b);

 

            public void Display()

            {

                Console.WriteLine("Abstract class");

            }

        }

 

         public class YY : XX

        {

            public override int subt(int a, int b)

            {

                return (a - b);

            }

 

            public override int sum(int a, int b)

            {

                return (a + b);

            }

 

            public void Show()

            {

                Console.WriteLine("YY class method");

            }

        }

 

        static void Main(string[] args)

        {

            YY obj = new YY();

 

            // here you can't create instance of a abstract class it is giving error

            //YY obj1 = new XX();

            //XX obj2 = new XX();

 

            // here we have used up level casting

            XX obj3 = new YY();

                       

            // here you don't have access to the Show method of YY

            // Because you can only access methods of a reference object only

            //obj3.Show();

 

            Console.WriteLine("Calling sum method through reference of base class " + obj3.sum(2,2));

 

            obj.Display();

 

            Console.WriteLine("Subtraction : "+ obj.subt(12, 10));

            Console.WriteLine("Addition : " + obj.sum(10, 10));

            Console.ReadLine();

 

        }

    }

}

 

You can define abstract to N levels but following consideration has to be made which is shown in the below practical demonstration:

 

Practical demonstration of abstract class levels:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace abstract_class_N

{

    class Program

    {

 

        public abstract class XX

        {

            public abstract int sum(int a, int b);

            public abstract int subt(int a, int b);

 

            public void Display()

            {

                Console.WriteLine("Abstract class");

            }

        }

 

        public abstract class YY : XX

        {

            public override int subt(int a, int b)

            {

                return (a - b);

            }

            public abstract override int sum(int a, int b);

        }

 

        public class ZZ : YY

        {

            public override int sum(int a, int b)

            {

                return (a + b);

            }

        }

 

        static void Main(string[] args)

        {

 

            ZZ obj = new ZZ();

 

 

            Console.WriteLine("Now we have complete the funtionality of sum method in ZZ class " + obj.sum(12, 12));

            Console.ReadLine();

 

        }

    }

}

 

You can have a private constructor in an abstract class but an inner class is used to initiate the abstract class constructor. In this case the abstract class can't be inherited.

 

Practical demonstration of private constructor in an abstract class:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace abstract_private_constructor

{

    class Program

    {

 

        public abstract class XX

        {

 

            private XX()

            {

                Console.WriteLine("Private constructor of class XX");

            }

 

            public abstract int sum(int a, int b);

            public abstract int subt(int a, int b);

 

            public static XX CreateInstance()

            {

                return new YY();

            }

 

            public class YY : XX

            {

                public override int subt(int a, int b)

                {

                    return (a - b);

                }

 

                public override int sum(int a, int b)

                {

                    return (a + b);

                }

            }

        }

 

        static void Main(string[] args)

        {

            // We have initiated the XX class with the help of static method for creating instance

            XX obj = XX.CreateInstance();

 

            // Here you can't directly create a instance of a abstract class which is having private constructor

            //XX ob2 = new XX();

 

            Console.WriteLine("Subtract method answer is " + obj.subt(12, 2));

            Console.WriteLine("Sum method answer is " + obj.sum(2, 2));

            Console.ReadLine();

        }

    }

}

 

Hope this article would have helped you in understanding abstract class and abstract methods in a simple way.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all