Showing posts with label example of abstract class in c#. Show all posts
Showing posts with label example of abstract class in c#. Show all posts

Monday, February 29, 2016

What is abstract class in c#?



An abstract class is an incomplete class that cannot be instantiated, it only allow other class to inherit from it.an abstract method must be implemented in child class by using the override keyword.

Features of abstract class: 

Abstract class have constant and fields
It can be implement a property
It can have constructor and destructors
It cannot support multiple inheritance
The example of the abstract class is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public abstract class Class1
    {
        protected string s;
        public abstract void show();
    }
    public class class2 : Class1
    {
        public override void show()
        {
            s = "Dot Net Darpan";
            MessageBox.Show(s);
         
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            class2 obj = new class2();
            obj.show();
        }
    }
}
 
Output: