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
{
interface ICircle
{
double Area(double radius);
}
interface IRectangle
{
double Area(double length, double breadth);
}
interface ITriangle
{
double Area(double basee, double height);
}
Example of
interface
namespace InterfaceApplication
{
public class InheritClass : ICircle,IRectangle,ITriangle
{
public void Print(string msg)
{
MessageBox.Show(msg);
}
double ICircle.Area(double radius)
{
return (3.14 * radius *
radius);
}
double IRectangle.Area(double length, double breadth)
{
return (length * breadth);
}
double ITriangle.Area(double basee, double height)
{
return (basee * height);
}
}
}
namespace InterfaceApplication
{
public partial class RectangleFrom : Form
{
public RectangleFrom()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
txtArea.Enabled = true;
IRectangle objRect = new InheritClass();
Doublearea= objRect.Area(Convert.ToDouble(txtLength.Text), Convert.ToDouble(txtWidth.Text));
txtArea.Text
= area.ToString();
}
}
}
namespace InterfaceApplication
{
public partial class CircleForm : Form
{
public CircleForm()
{
InitializeComponent();
}
private void bntSubmit_Click(object sender, EventArgs e)
{
txtArea.Enabled =true ;
ICircle objInteritClass = new InheritClass();
double radius
=objInteritClass.Area(Convert.ToDouble(txtRadius.Text));
txtArea.Text
= radius.ToString();
}
}}
namespace InterfaceApplication
{
public partial class TriangleForm : Form
{
public TriangleForm()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
lblTriArea.Enabled = true;
ITriangle objInheritClass = new InheritClass();
Double area=objInheritClass.Area(Convert.ToDouble(txtBase.Text),
Convert.ToDouble(txtHeight.Text));
txtArea.Text = area.ToString();
}
}
}
namespace
InterfaceApplication
{
public partial class InterfaceForm : Form
{
public InterfaceForm()
{
InitializeComponent();
}
private void btnCircle_Click(object sender, EventArgs e)
{
CircleForm objCircleForm = new CircleForm();
objCircleForm.Show();
}
private void btnRectangle_Click(object sender, EventArgs e)
{
RectangleFrom objCircleForm = new RectangleFrom();
objCircleForm.Show();
}
private void btnTriangle_Click(object sender, EventArgs e)
{
TriangleForm objTriangleForm = new TriangleForm();
objTriangleForm.Show();
}
}
}
Leave Comment