Abstraction is general means dealing with ideas rather the events. Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. In the same way that abstraction sometimes works in art, the object that remains is a representation of the original, with unwanted detail omitted.
An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.
An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.
Code Example :
AbstractClass.cs
// We have made an abstract class with a abstract method add that will return int type data, taking two parameters, notice that only method definition is present without a body and the class as well as the method are marked abstract. Abstract method is always followed by a semicolon (;). Also keep in mind that an abstract class can have non - abstract method but abstract method are supposed to present within the abstract class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ClassInCsharp
{
public abstract class AbstractClass
{
public abstract int add(int a, int b);
}
}
usingAbstract.cs
// Here we have inherited our abstract class and have declared the body of the add method by using a override keyword. Always remember that the child class has to override all the abstract method of parent class if any.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ClassInCsharp
{
public class UsingAbstract:AbstractClass
{
public override int add(int a, int b)
{
int c = a + b;
return c;
}
}
}
AbstractExample.aspx
// We have developed a GUI with three textbox with id txtNo1, txtNo2, txtResult and a button to perform the addition operation. Here we are using that abstract method by calling add method with the help of UsingAbstract class (child class).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ClassInCsharp
{
public partial class AbstractExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
UsingAbstract abs = new UsingAbstract();
int result = abs.add(int.Parse(txtNo1.Text), int.Parse(txtNo2.Text));
txtResult.Text = result.ToString();
}
}
}
No comments:
Post a Comment