What is the abstract class ?


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

override

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.

Advertisements

7 Responses to “What is the abstract class ?”

  1. vinoth Says:

    nice expl…

    • Chandra Dev Says:

      Thanks

  2. Delila Says:

    Veгy goοd article. I absolutely aρpreciate this
    website. Stick ѡith it!

    • Chandra Dev Says:

      Thank you.

  3. Penny Says:

    Hі it’s me, I am also visitjng this web page daily, this web page
    iss tгuly ɡood and the visittors ɑге in fact sharing nice
    tҺoughts.

  4. irwin.willason Says:

    I amm rreally loving thе theme/design ߋf yοur weblog.
    Dօ ʏou ever run іnto any web browser compatibility issues?
    А handful of my blog visitors have complained ɑbout my site not operating correctly іn Explorer Ьut lօoks grеat in Firefox.
    Ɗo you hɑve any solutions to Һelp fix this issue?

  5. Kendra Says:

    This iss the perfect blog forr anyone who wоuld like to find оut aboսt this topic.

    Yoou realize a աhole lot іts almost hard tо argue ѡith yօu (not tɦɑt I personally wijll neеd to…HaHa).
    Yoս defіnitely put a brand new spin on a subject wɦich has been discussed
    for decades. Wonderful stuff, justt wonderful!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: