Types of classes part-2 - Abstract class - DotNet and DataBase

Saturday, 14 July 2018

Types of classes part-2 - Abstract class





In this post we will discuss on Abstract Class, Static class and Generic class.

Abstract class:
Abstract class is a class that must be inherited and have the methods of overloading. We can't inherit the abstract to some other class, by creating object to the abstract class we can use the members in other class.

Syntax:


abstract class abClass
    {
    }

Abstract method:
Abstract method we define under the abstract class. Abstract method contains only definition. Body (logic/code) we create in the derived class by overriding.
An abstract class can contain both abstract and non abstract methods.

Syntax:


public abstract return type methodname()
    {
    }

Note:  We must override the abstract method in the derived the class. .Net CLR forces the user to override the abstract method.

Example





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClass
{
    abstract class abClass
    {
        public void abMethod1()
        {
            Console.WriteLine("I'm a non abstract method under abClass");
            //Console.Read();
        }
        public abstract void abMethod2();
    }

    class Program : abClass
    {
        public override void abMethod2()
        {
            Console.WriteLine("I'm a abstract method under abClass");
            Console.Read();
        }
        public void ccProgram()
        {
            abMethod1();
        }

        static void Main(string[] args)
        {
            Program ab = new Program();
            ab.ccProgram();
            ab.abMethod2();
        }
    }    
}



if we not override the abstract class in the derived class, we get the compile time error as below



Note:
Abstract method should public.










No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.