Abstract Class and Abstract Method in C#

By
Abstract Class:- 
we use 'abstract' keyword to implement the concept of abstract class. 'Abstract class' can only inherited by other class.we can not create objects of 'abstract class' directly. When any  'abstract class' is inherited by other class then we create the objects of that derived class.We can not create objects of  'abstract class' because methods of  'abstract class' only declare but not define.
    If any class will inherited the  'abstract class' then that derived class will give the body of the  'abstract class' member.
Some characteristics of an  'abstract class' are:-
  • It can not be instantiate directly.
  • It can have abstract members.
  • we can not apply a sealed modifier to it.
Example:- 

abstract class Base
{
.............................
.............................
}
class Derived:Base
{
.............................
.............................
}

Main Method()
{
Base b1;                      // Error           
Derived d1;                // Ok
}
Abstract Method:-
when we know what will happen but do not know how it happen ,we just only declare the method but do not define the method. In that case abstract keyword declare with method.
when an instance method declaration includes the modifier abstract,the method is said to be an abstract method.
Example:-
public abstract void add(int x, int y)
Some characteristics of abstract method are:-




  • It can not have implementation.





  • Its implementation must be provide in non-abstract derived classes by overriding the method.





  • It can be declared only in abstract classes.





  • It can not take either static or virtual modifier.





  • An abstract declaration is permitted to override a virtual method.
  • Example:-
    There are Example of  Abstract class, Abstract Method,Abstract Property and  Abstract  Indexer.
    
    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 ABSTRACT_KEYWORD
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                st obj = new st();
                obj[0] = "hindi";
                obj.PropName = "vijay";
                MessageBox.Show(obj[0]);
                MessageBox.Show(obj.PropName);
                obj.show("student");
                obj.add(5,8);
            }
        }
    public abstract class student
    {
        public abstract void show(string s);
        public abstract string PropName
        {
            get;
            set;
        }
        public abstract string this[int i]
        {
            get;
            set;
        }
        public void add(int i, int ii)
        {
            int sum = i + ii;
            MessageBox.Show(sum.ToString());
        }
    }
        public class st:student
        {
            string sname;
            string[] marks=new string[5];
            public override void show(string s)
            {
                MessageBox.Show("hello"+s);
            }
            public override string PropName
            {
                get
                {
                    return sname;
                }
                set
                {
                    sname=value;
                }
                }
              public override string this[int i]
              {
        
                get
                {
                    return marks[i];
                }
                set
                {
                    marks[i]=value;
    
                }
              }
        } 
     }
    
    There are some steps to Execute the above program.
    Step1:- First open your visual studio-> Click File->click New Project-> Select windows Form Application->Click Ok->
    see it:-

    Step2:-  Now drag and drop a button from the toolbox.
    see it:-

    Step3:- Now double click on button1->write the above program code.
    see it:-

    Step4:- Run the program (press (F5))->click button1
    OUTPUT:-

    Now click ok button one by one you will see following output.
    see it:-


    For more:-

    1.   E-Post System Project
    2. Out Parameter
    3.    File handling  Real Application
    4.   Generate unique Number and save it in the database
    5.    Host ASP.NET Application on Server Free
    6. oops concepts
    7. Create a Setup File

    To Get the Latest  Free Updates Subscribe
    I hope this helpful for you.
    Click the download button to download whole application.
               DOWNLOAD

    0 comments:

    Post a Comment

    Powered by Blogger.