Thursday 26 January 2017

ABSTRACT CLASSES IN C#

ABSTRACT CLASSES IN C#

Abstract classes

The abstract keyword is used to create abstract classes

An abstract class is incomplete and hence cannot be instantiated

An abstract class can only be used as base class

An abstract class can not be sealed

An abstract class may contain abstract members(methods, properties,indexers,and events) but not mandatory.

A non- abstract class derived from an abstract class must provide implementations for all inherited abstract members.

If a class inherits an abstract class, there are 2 options available for that class

Option 1: Provide Implementation for all the abstract members inherited from the base abstract class.

Option 2: If the class does not wish to provide Implementation for all the abstract members inherited from the abstract class, then the class has to be marked as abstract.

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

namespace ConsoleApplication4
{
    public abstract class Customer
    {
        public abstract void print();

    }

    public class program : Customer
    {
        public override void print()
        {
            Console.WriteLine("Hello abstract method");
        }
        public static void Main()
        {
            program p = new program();
            p.print();
            Console.ReadLine();
        }
    }
}


1 comment:

Note: only a member of this blog may post a comment.