Introduction: Abstract Class and Interface is one of the most
frequently asked interview question. In this tutorial, I
will explain about Abstract class in C#. To learn about Interface visit What is Interface in C# and Where to Use it
Abstract Class:
Just like class, The Abstract keyword is used
to create Abstract classes. Abstract classes are incomplete and hence can’t be
instantiated. An Abstract class can only be a base class. An abstract class can
have both abstract and non-abstract Methods (concrete methods).
Example:
abstract class Customer // Using abstract keyword to create abstract
class.
{
public void Print1() // Non Abstract method
{
}
public abstract void Print2(); // Abstract method
}
If we declare body of abstract method then it will give error
because we can’t declare body of Abstract class.
Example:
abstract class Customer // Using abstract keyword to create abstract
class.
{
public void Print1() // Non Abstract method
{
}
public abstract void Print2(); // Declaring body for
Abstract
method
// Error-Invalid
token '{' in class, struct,
// or interface member declaration
// or interface member declaration
{
}
}
Since Abstract classes are incomplete class So it doesn’t make sense
to create instance of Abstract class. Abstract class can’t be
instantiated they can only be used as a base class.
Example:
namespace ConsoleApplication
{
abstract class Customer // Using abstract keyword to create abstract
class.
{
public void Print1() // Non Abstract method
{
}
public abstract void Print2(); // Abstract method
{
}
}
class Program
{
static void Main(string[]
args)
{
Customer c = new Customer(); //can't create instance of Abstract
// class or Interface 'Customer'
// class or Interface 'Customer'
}
}
}
If a class is inherited from Abstract class then that class would
be responsible to define that abstract method, otherwise it will give error.
Example:
abstract class Customer
{
public abstract void Print2(); // Abstract method
}
class Program : Customer
{
static void Main(string[]
args)
{
// Program doesn’t implemented Abstract method
}
}
If a class inherits an abstract class, then there are two options
available for that class
Option 1: Provide implementation
for all the abstract members inherited from the base abstract class.
Example:
abstract class Customer
{
public abstract void Print2(); // Abstract method
}
class Program : Customer
{
public override void Print2()
{
Console.WriteLine("Print2 method");
}
static void Main(string[]
args)
{
}
}
Option 2: If the class doesn’t
wish to provide implementation for all the abstract members inherited from the
abstract class, then the class has to be mark as abstract.
Example:
abstract class Customer
{
public abstract void Print2(); // Abstract method
}
abstract class Program : Customer //
Marked as Abstract
{
static void Main(string[]
args)
{
}
}
We can’t create instance of Abstract class but Abstract class
reference variable can point to base class object.
Example:
abstract class Customer
{
public abstract void Print2(); // Abstract method
}
class Program : Customer
{
public override void Print2()
{
Console.WriteLine("Print2 method");
}
static void Main(string[]
args)
{
Customer objCustomer = new Program();//
base class reference variable
//pointing derived class object
}
}
Abstract class can’t be marked as sealed because they contradicted
to each other.
Example:
abstract class Customer
{
public abstract void Print2(); // Abstract method
}
Abstract class means Customer class always can be used as base
class.
While
Sealed means Customer class cannot used as base class.
abstract sealed class Customer // An Abstract class cannot be sealed or static
{
public abstract void Print2();
}
An Abstract class may contain abstract members (Method,
properties, indexers, and Events), But not mandatory.
When to use Abstract classes:
When you have a requirement where your base
class should provide default implementation of certain methods whereas other
methods should be open to being overridden by child classes use abstract
classes.
The purpose of an abstract class is to provide a common
definition of a base class that multiple derived classes can share.
For example a class library may define an abstract class that
is used as a parameter to many of its functions and require programmers using
that library to provide their own implementation of the class by creating a
derived class.
Real Life example:
Your boss assigned you to create a registration page for your
project then he marked 'Create
registration page' as
Abstract. Now you are responsible to create that page in your own style.
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..