blog

Home / DeveloperSection / Blogs / Difference between Interface and Abstract Class in .Net

Difference between Interface and Abstract Class in .Net

priyanka kushwaha2214 23-Jan-2015

Difference between Interface and Abstract Class in .Net

In this blog, I’m explaining about interface and abstract class in .Net

Abstract Class

Abstract class contains incomplete and complete both type of method. It need to extended and implemented. It cannot be instantiated.

Abstract Class have the following feathers.

1.     An abstract class cannot be instantiated.

2.     An abstract class contain abstract members as well as non abstract member .

3.     An abstract method is implicitly a virtual method.

4.     It is an error to use the abstract modifier or static property.           

5.     An abstract class can has constructor.

6.     An abstract class cannot be inherited by structures.

 Example:

Namespace AbstractClass

{

public abstract  class BaseClass

    {public BaseClass(string msg)

        {

              System.Console.WriteLine(msg);

        }

        public  void display(double res)

        {

            System.Console.WriteLine("Result="+res);

        }

     abstract   public double sum(double var1, double var2);

    }

   public class DriveClass:BaseClass

    {

          public  DriveClass() : base("this is base class")

          {}

       public override double sum(double var1, double var2)

           {

             return (var1 + var2);

            }

    }

    class Program

    {

        static void Main(string[] args)

        {

            DriveClass objcs = new DriveClass();

            double var1,var2;

            Console.WriteLine("Enter first no.");

            var1=Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter Second no.");

            var2 = Convert.ToDouble(Console.ReadLine());

            double result=objcs.sum(var1,var2);

            objcs.display(result);

          }

    }

}

  class Program

{

               static void Main(string[] args){

            DriveClass objcs = new DriveClass();

            double var1,var2;

            Console.WriteLine("Enter first no.");

            var1=Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter Second no.");

            var2 = Convert.ToDouble(Console.ReadLine());

            double result=objcs.sum(var1,var2);

            objcs.display(result);

           }

       }

}

Interface

An interface contains only prototype of  method,  properties, events or indexers. It has no implementation. Implementation  of the methods is done by the class that which implements the interface.

Properties of interface

1.     Interface must be declared with  the key  word ‘interface’.

2.     It contains only incomplete method.

3.     Interface does provides facility for multiple inheritance.

4.     Interface can not contains constructors.

5.     Member of interface cannot be static.

 

Create an Interface Class

namespace InterfaceApplication

{

interfaceICircle

{

 double Area(double radius);

}

interfaceIRectangle

{

double Area(double length, double breadth);

}

interfaceITriangle

{

double Area(double basee, double height);

}

Example of interface

 

namespace InterfaceApplication

{

    publicclassInheritClass : ICircle,IRectangle,ITriangle

    {

     

        publicvoid Print(string msg)

        {

            MessageBox.Show(msg);

        }

 

        doubleICircle.Area(double radius)

        {

             return (3.14 * radius * radius);

        }

 

        doubleIRectangle.Area(double length, double                            breadth)

        {

            return (length * breadth);

        }

 

        doubleITriangle.Area(double basee, double height)

        {

            return (basee * height);

        }

      }

}

 

 

namespace InterfaceApplication

{

    publicpartialclassRectangleFrom : Form

    {

        public RectangleFrom()

        {

            InitializeComponent();

        }

 

        privatevoid btnSubmit_Click(object sender, EventArgs e)

        {

            txtArea.Enabled = true;

            IRectangle objRect = newInheritClass();

            Doublearea= objRect.Area(Convert.ToDouble(txtLength.Text),              Convert.ToDouble(txtWidth.Text));

            txtArea.Text = area.ToString();

           

        }

    }

}

namespace InterfaceApplication

{

    publicpartialclassCircleForm : Form

    {

        public CircleForm()

        {

            InitializeComponent();

        }

 

        privatevoid bntSubmit_Click(object sender, EventArgs e)

        {

            txtArea.Enabled =true ;

            ICircle objInteritClass = newInheritClass();

             double radius =objInteritClass.Area(Convert.ToDouble(txtRadius.Text));

             txtArea.Text = radius.ToString();

            

        }

    }}

 

namespace InterfaceApplication

{

    publicpartialclassTriangleForm : Form

    {

        public TriangleForm()

        {

            InitializeComponent();

        }

 

        privatevoid btnSubmit_Click(object sender, EventArgs e)

        {

            lblTriArea.Enabled = true;

            ITriangle objInheritClass = newInheritClass();

            Double  area=objInheritClass.Area(Convert.ToDouble(txtBase.Text), Convert.ToDouble(txtHeight.Text));

            txtArea.Text = area.ToString();

           

        }

    }

}

 

namespace InterfaceApplication

{

    publicpartialclassInterfaceForm : Form

    {

        public InterfaceForm()

        {

            InitializeComponent();

        }

 

        privatevoid btnCircle_Click(object sender, EventArgs e)

        {

            CircleForm objCircleForm = new CircleForm();

            objCircleForm.Show();

        }

 

        privatevoid btnRectangle_Click(object sender, EventArgs e)

        {

            RectangleFrom objCircleForm = new RectangleFrom();

            objCircleForm.Show();

 

        }

 

        privatevoid btnTriangle_Click(object sender, EventArgs e)

        {

            TriangleForm objTriangleForm = new TriangleForm();

            objTriangleForm.Show();

        }

       }

}

 


Updated 23-Jan-2015

Leave Comment

Comments

Liked By