Abstract Class - Explained

SheoNarayan
Posted by in C# category on for Intermediate level | Views : 59904 red flag
Rating: 5 out of 5  
 2 vote(s)

Questions on Abstract class are very frequently asked in interviews:) Apart from interviews Abstract class is also very important to know when you are designing or working on a real time applications that needs proper design. I am not expert in this however trying to explain what I know out of my limited knowledge. This article tries to cover Abstract class, Abstract method, Abstract property and difference between abstract method and virtual method.

Abstract class is a class that can not be instantiated. To use it you need to inherit it. This class can be used as a base class where you can define certain method that must be implemented in derived class (Class that is going to inherit it) along with that you can also define certain methods that is frequently used and can be directly used by Derived class or overriden in the derived class if needed.

In the abstract class, you can define following:

  1. Normal property - this property is similar to any other property we define in a normal class
  2. Abstract property - this will have only get and set accessor but no implementation.
  3. Normal method - this method is similar to any other method that you define in a normal class
  4. Abstract method - this will not have any implementation
  5. Virtual method - this will have implementation but can also be overridden in the derived class to provide additional logic or completely replace its logic
Abstract Class

In order to explain Abstract class, I am going to take a simple example of ParentClass class that has all methods and properties explained above and it looks like below

ParentClass.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for ParentClass

/// </summary>

public abstract class ParentClass

{

public ParentClass()

{

}

private int age = 0;

// Normal property

public int Age_Normal

{

get

{

return age;

}

set

{

age = value;

}

}

// Abstract property

public abstract string Address_Abstract

{

get;

set;

}

// Normal Methods

public string GetName(string firstName, string lastName)

{

return "My Name is : " + GetName_Virtual(firstName, lastName);

}

public int Divide_NotAbstract(int a, int b)

{

return a / b;

}

// Abstract Methods

public abstract int Add_Abstract(int a, int b);

public abstract int Subtract_Abstract(int a, int b);

 

// Virtual method

public virtual string GetName_Virtual(string firstName, string lastName)

{

return firstName + " " + lastName;

}

}

Get solutions of .NET problems with video explanations, .pdf and source code in .NET How to's.

As you can see, the first property I have above is Age_Normal, this is a normal property similar to other property that we define in the class that has its implementation as well and it can be simply accessed by the instance of the dervied class.

Next we have an Abstract property Address_Abstract that has only get and set accessor and no implementation.

Next we have normal method that is similar to any other method we define in a normal class.

Next we have Abstract methods that contains abstract keyword in its definition, this doesn't have any implementation. (Defining abstract methods and properties are similar to defining properties and methods in an interface). One thing to note is that an abstract methods or properties can only be defined in Abstract class, you can't define them in a normal class.

Next we have a virtual method that let us use its logic directly or also allow us to completely override its logic.

Derived Class

Below is my derived class (that is inheriting above abstract class), its name is DerivedClass (Some of the method or property names may look strange, I have just kept this for easy understanding, please bear with me).

DerivedClass.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for Calculate

/// </summary>

public class DerivedClass : ParentClass

{

private string address = string.Empty;

public DerivedClass()

{

}

// override the abstract property

public override string Address_Abstract

{

get

{

return address;

}

set

{

address = value;

}

}

// override the abstract method

public override int Add_Abstract(int a, int b)

{

return a + b;

}

public override int Subtract_Abstract(int a, int b)

{

return a - b;

}

// override virtual method

public override string GetName_Virtual(string firstName, string lastName)

{

return "(Overriden) Name: " + firstName + " " + lastName;

}

// hide normal method of the abstract class

public new string Divide_NotAbstract(int a, int b)

{

int d = a/b;

return "The division is: " + d.ToString();

}

// use abstract property to retrieve its value

public string GetAddress()

{

return Address_Abstract;

}

}

In the above class I am doing following:

First, I am overriding the Address_Abstract property, as I had declared it as Abstract property. Note that to implement the abstract property or method, you need to use override keyword.

Next, I am overriding two methods Add_Abstract and Sub_Abstract as these methods are declared as Abstract method.

Next I had declared GetName_Virtual method as virtual method so I have freedom to either override it or use it as it is. If we want to use the original method, we can choose not to override it but if we want to modify its logic then we will have to override it. I preferred to override so I have redefined my logic here.

Next I have made Divide_NoAbstract method of Parent class as hidden by specifying new keyword in the definition of derived class. Even if I have made the original parent class method as hidden if at some place we want to use the orginial abstract class method, we can use that, I will show you how to use that later.

How to use Derived and Abstract class methods

Below is the code that shows how to use above abstract class and derived class methods or properties.

DerivedClass c = new DerivedClass();

Response.Write("<b>Abstract method - Sum: </b>" + c.Add_Abstract(50, 30).ToString() + "<br />");

Response.Write("<b>Abstract method - Subtract: </b>" + c.Subtract_Abstract(50, 30).ToString() + "<br />");

Response.Write("<b>Virtual Method - GetName_Virtual: </b>" + c.GetName_Virtual("SHEO", "NARAYAN") + "<br />");

Response.Write("<b>Normal Public Method - GetName: </b>" + c.GetName("SHEO", "NARAYAN") + "<br />");

Response.Write("<b>Normal Public Method being hidden using new keyword - Divide_NotAbstract: </b>" + c.Divide_NotAbstract(50, 30).ToString() + "<br />");

ParentClass p = new DerivedClass();

Response.Write("<b>Normal Public Method from Abstract Class - Divide_NotAbstract: </b>" + p.Divide_NotAbstract(50, 30).ToString() + "<br />");

c.Address_Abstract = "Sheo Narayan, Hitec City, Hyderabad.";

Response.Write("<b>Normal Public method accessing <br />overriden Abstract Property - GetAddress: </b>" + c.GetAddress() + "<br />");

Above code snippet will give following results


Abstract method - Sum: 80
Abstract method - Subtract: 20
Virtual Method - GetName_Virtual: (Overriden) Name: SHEO NARAYAN
Normal Public Method - GetName: My Name is : (Overriden) Name: SHEO NARAYAN
Normal Public Method being hidden using new keyword - Divide_NotAbstract: The division is: 1
Normal Public Method from Abstract Class - Divide_NotAbstract: 1
Normal Public method accessing
overriden Abstract Property - GetAddress:
Sheo Narayan, Hitec City, Hyderabad.

In the above code snippet, first I instantiated the DerivedClass method and start calling the both Abstract methods Add_Abstract and Subtract_Abstract that were implemented in the Derived class.

Next I have called the Virtual method of the Abstract class that was overriden in the Derived class. You can see the implementation of GetName_Virtual method in the Derived class that is prefixing "(Overriden) Name" with the result.

Next line is calling a normal method GetName that was defined in the Parent abstract class.

Next line is calling the method Divide_NoAbstract that is hiding the main normal method Divide_NoAbstract of Parent class by specifying the new keyword in its definition in the derived class. 

Now lets suppose, you already have made the Parent class method (as explained in the above line) hidden but still in a certain scenario, you want to call the Parent class method. To do that, we need to instantiate the Derived Class by specifying it as the ParentClass by writing it as ParentClass p = new DerivedClass();. This is giving me the reference of Parent class and when called Divide_NoAbstract method, this will call the Parent class Divide_NoAbstract method not Derived class method.

The very next line is setting the property of the Derived class that is nothing but the implementation of the ParentClass Address_Abstract property and calling the Derived class GetAddress method that is simply returning this property value.

Conclusion

In this article I tried to show a practical example of how to use Abstract class and what are differnt things that we can define into it. I also tried to show how to work with methods and properties of Abstract class in different scenarios. Hope this article will be useful for the readers. Please subscribe for the subsequent articles alert directly in your email. NOTE: This article was written in 3 breaks, please let me know if somewhere the continuation is broken. Thanks

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)