Hi
This is one of the frequently asked questions in interview. I have faced this question in almost in all interviews. So let’s understand what is abstract class?
Abstract class is the class which one cannot be instantiated. This means we canot create object of this class like general class. To use it, we need to inherit it. It contains the abstract keyword.
In abstract class, we can define
1. Normal Property
2. Abstract Property
3. Normal Method
4. Abstract Method
5. Virtual method.
Now let see this concept with example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
public abstract class Class1
{
public virtual string SayHello()
{
return "Hello from virual method";
}
public string sayHello2()
{
return "I m from SayHello Method";
}
public abstract string SayHello1();
}
Note: Here i have created the abstract class which contain Normal method,abstract method and Virtual method.
Now we will inherit this class in class2 like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class2
/// </summary>
public class Class2:Class1
{
public override string SayHello()
{
return "This is the orveride message using virtual method";
}
public override string SayHello1()
{
return "This is the orveride message using abstract message";
}
}
Note: in the above code i have override the virtual and abstract method. So if virtual or abstract keyword is there then we have to must override it.
Now we will call this method in default.cs page like this on button click event
protected void Button1_Click(object sender, EventArgs e)
{
Class2 obj = new Class2();
Response.Write(obj.SayHello()+"<br>");
Response.Write(obj.SayHello1());
}
Now compile the code, we will get output like this

When to use abstract Class?
We can use this class in this senario
1. If we have created some class and we want to modify some methods with new feature and we have to use somewhere then we can use abstract class
2. If we are developing some product type project, where we have to keep on adding new features in coming days then we can use abstract class.
Note: This is all my understanding my abstract class, if you feel i missing some important point then let me know. You are always welcome.
12.971599
77.594563