Home > DeveloperSection > Articles > Abstraction in c#

Abstraction in c#


C# C# 
Ratings:
0 Comment(s)
 3124  View(s)
Rate this:

Abstraction in c#

Abstract class is having abstract methods. Abstract classes provide a way to force an inherited class to implement and override these abstract methods.

Example of abstraction:

For example a create flower class. This class contains a method that is defined abstract mean no definition only declaration. There is other two another classes Rose and Lilly that inherits the Flower class. These child classes must override the abstract function of flower class. There is Flower class is not directly instantiated because it is abstract class.

Abstraction in c#

Write down following line of code:

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 abstraction

{

    public partial class frmabstraction : Form

    {

        public frmabstraction()

        {

            InitializeComponent();

        }

 

        public abstract class Flower  //abstract class is created.

        {

            public string FlowerColor   //set the property of variables

            {

                get;

                set;

            }

            public int FlowerSize

            {

                get;

                set;

            }

           

            public Flower() //constructor created

            {

                FlowerColor = "no color";

                FlowerSize = 0;

            }

            public abstract void GetDescription(string s);  //abstract function is created.

      

        }

        public class Rose : Flower  //Rose class created inherite flower class

        {

            public string Description;

            public override void GetDescription(string s)   //override the function of base class Flower

            {

                Description = s;

            }

        }

        public class Lilly : Flower  //Lilly class created inherite flower class

        {

            public string Description;

            public override void GetDescription(string s)   //override the function of base class Flower

            {

                Description = s;

               

            }

        }

 

        private void buttonSubmit_Click(object sender, EventArgs e)

        {

          

            Rose R = new Rose();        //object created of Rose class

            R.FlowerColor = txtColor.Text;

            R.FlowerSize = Int32.Parse(txtSize.Text);

            R.GetDescription(txtDescription.Text);

           

 

            MessageBox.Show("Rose Details..\nRosecolor.."+R.FlowerColor+"\nRoseSize..."+R.FlowerSize+"\nRoseDescription.."+R.Description);

        }

 

        private void buttonLilly_Click(object sender, EventArgs e)

        {

            Lilly L = new Lilly();  //object created of Lilly class

            L.FlowerColor = txtColor.Text;

            L.FlowerSize = Int32.Parse(txtSize.Text);

            L.GetDescription(txtDescription.Text);

            MessageBox.Show("Lilly Details..\nLillycolor.." + L.FlowerColor + "\nLillySize..." + L.FlowerSize + "\nLillyDescription.." + L.Description);

        }

    }

}

 

 

Run the program and give the details.

Abstraction in c#

Click on button ‘Rose’.

Abstraction in c#

This message box display details of rose by creating Rose class object.

Abstraction in c#

Again fill the Lilly details and click on the Lilly button.

Abstraction in c#

This message box displays the Lilly flower details by creating Lilly class object.


Don't want to miss updates? Please click the below button!

Follow MindStick