Abstract class in C#.NET
An abstract class is one of the special behaviored feature provided by the .NET. It is a class which cannot be instantiated. You can inherit this class but you cannot create an object for this class, means you can represent abstract class is like a base class and implement the base class features in derived class. Learn more on Abstract class in C#.NET
Know more about Abstract class in C#.NET
You can ask the question why we need a class without permitting us to create an object? Yes, the base class enforces the derived classes to implement certain features in a child class, we can say it a type of agreement.
A small example of Abstract class declaration in C#.
abstract class myAbstractClass
{
}
An abstract class can contain both abstract members and non-abstract members. You may have the implementation for the non-abstract members, but abstract members should not have the implementation in the abstract class. What I mean here is, Only method signature should be there for the non-abstract members, these abstract members should compulsory implement in the derived classed to enforce the base class rules.
abstract class myAbstractClass
{
public void myMessage()
{
Console.WriteLine("This is non abstract member");
}
}
Below is the sample program which explains the abstract class implementation. Here I have declared a class myAbstractClass with abstract key word means that is an abstract class, in that class I have mentioned concrete method is AddTwoNumbers and abstract method is MultiplyThreeNumbers. I have implemented the AddTwoNumbers() method in the same abstract class as that is non-abstract method and I have derive the abstract from myDerivedClass class. Now I have implemented the method MultiplyThreeNumbers() in derived class by using a key word Override.
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 WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myDerivedClass bojDerived = new myDerivedClass();
int sum = bojDerived.AddTwoNumbers(100, 2,30);
int multiplication = bojDerived.MultiplyTwoNumbers(100, 2, 30);
}
abstract class myAbstractClass
{
//A concrete method
public int AddTwoNumbers(int Num1, int Num2, int Num3)
{
return Num1 + Num2 + Num3;
}
//An abstract method, which should be overridden in derived class
public abstract int MultiplyThreeNumbers(int Num1, int Num2,int Num3);
}
//A Child Class of myAbstractClass
class myDerivedClass : myAbstractClass
{
public override int MultiplyThreeNumbers(int Num1, int Num2,int Num3)
{
return Num1 * Num2*Num3;
}
}
}
}Overriding:
Method overriding is a c# function feature that allows you to extend the functios in the same heirarchy of inherited functions.The override modifier is useful to extend or modify the abstract or virtual implementation of an inherited method.
Now on the button click event I have created an instance for the class myDerivedClass and called methods in that class providing the parameters.Points to be remember:
1) An abstract class is for not to create an object.
2) An abstract class may contain abstract members and concrete members.
3) Need to use the Override keyword in the derived class implementation.
4) You should only declare the abstract members in abstract class.
5) Cannot use the virtual, static keywords in abstract declaration.
6) Can not inherit the multiple abstract classes in derived class.
An abstract calss can inherit from a class and one or more interfaces.
An abstract class can implement code with non-Abstract methods.
Abstract class can have modifiers for methods,properties etc.,
Abstract class can have constant and fields.
An abstract class can implement a property.
An abstract class can have constructors or destructors.
An abstract class cannot be inherited from by structures.
An abstract class cannot support multiple inheritance.