Introduction
In this article, I will explain about abstract class and abstract methods.
What are Abstract Class and Abstract Methods?
Also called an "abstract superclass" in object technology, it is a class created as a master structure. No objects of an abstract class are created; rather, subclasses of the abstract class are defined with their own variations, and the subclasses are used to create the actual objects.
Something about Abstract Classes.
- Class is an abstract type
- Class is a template
- Class is a user-defined type.
- Class is an abstract type.
What is an Abstract class?
- The abstract is incomplete or can say partial information.
- We can't directly initiate an abstract class because it is incomplete (It doesn't have complete functionality).
- It is initiated through a derived class when complete functionality is done.
- An abstract class has partial information, which is fully done in the derived class.
- An abstract class may have abstract methods or complete methods.
- An abstract class can have a constructor, which is used to initialize data members of the abstract class, which will be initiated indirectly with the help of the derived class.
- If an abstract class has a private constructor, we have to create an inner class to initialize the abstract class.
Features of Abstract Class
- Abstract class can't be static.
- An abstract class is used for inheritance.
- Abstract class can't be sealed.
- Abstract or virtual members can't be private, as we can't override it.
- An abstract class can be inherited from an abstract class, but the methods in the base class have to be declared abstract.
Features of Abstract Methods
- An abstract method is implicitly a virtual method.
- Abstract method declarations are only permitted in abstract classes.
- The implementation is provided by an overriding method, which is a member of a non-abstract class.
- It is an error to use the static or virtual modifiers in an abstract method declaration.
- An abstract method declaration provides no actual implementation; there is no method body; the method declaration simply ends with a semicolon e.g.: public abstract void Display();
Note. In inheritance, we can't change the access modifier of a member method in the lower level because it has to check for method declaration (which method to override i.e. signature identification).
Practical demonstration of an Abstract class.
using System;
using System.Collections.Generic;
namespace AbstractClassExample
{
class Program
{
public abstract class XX
{
public abstract int Sum(int a, int b);
public abstract int Subtract(int a, int b);
public void Display()
{
Console.WriteLine("Abstract class");
}
}
public class YY : XX
{
public override int Subtract(int a, int b)
{
return (a - b);
}
public override int Sum(int a, int b)
{
return (a + b);
}
public void Show()
{
Console.WriteLine("YY class method");
}
}
static void Main(string[] args)
{
YY obj = new YY();
// You can't create an instance of an abstract class
// YY obj1 = new XX();
// XX obj2 = new XX();
// Upcasting
XX obj3 = new YY();
// You don't have access to the Show method of YY through the base class reference
// obj3.Show();
Console.WriteLine("Calling Sum method through reference of the base class: " + obj3.Sum(2, 2));
obj.Display();
Console.WriteLine("Subtraction: " + obj.Subtract(12, 10));
Console.WriteLine("Addition: " + obj.Sum(10, 10));
Console.ReadLine();
}
}
}
You can define abstract to N levels, but the following consideration has to be made, which is shown in the below practical demonstration.
Practical demonstration of Abstract class levels.
using System;
using System.Collections.Generic;
namespace AbstractClassExample
{
class Program
{
public abstract class XX
{
public abstract int Sum(int a, int b);
public abstract int Subtract(int a, int b);
public void Display()
{
Console.WriteLine("Abstract class");
}
}
public abstract class YY : XX
{
public override int Subtract(int a, int b)
{
return (a - b);
}
public abstract override int Sum(int a, int b);
}
public class ZZ : YY
{
public override int Sum(int a, int b)
{
return (a + b);
}
}
static void Main(string[] args)
{
ZZ obj = new ZZ();
Console.WriteLine("Now we have completed the functionality of the Sum method in ZZ class: " + obj.Sum(12, 12));
Console.ReadLine();
}
}
}
You can have a private constructor in an abstract class, but an inner class is used to initiate the abstract class constructor. In this case, the abstract class can't be inherited.
Practical demonstration of private constructor in an Abstract class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstract_private_constructor
{
class rogram
{
public abstract class XX
{
private XX()
{
Console.WriteLine("Private constructor of class XX");
}
public abstract int sum(int a, int b);
public abstract int subt(int a, int b);
public static XX CreateInstance()
{
return new YY();
}
public class YY : XX
{
public override int subt(int a, int b)
{
return (a - b);
}
public override int sum(int a, int b)
{
return (a + b);
}
}
}
static void Main(string[] args)
{
// We have initiated the XX class with the help of static method for creating an instance
XX obj = XX.CreateInstance();
// Here you can't directly create an instance of an abstract class which is having a private constructor
// XX ob2 = new XX();
Console.WriteLine("Subtract method answer is " + obj.subt(12, 2));
Console.WriteLine("Sum method answer is " + obj.sum(2, 2));
Console.ReadLine();
}
}
}
Hope this article has helped you in understanding abstract class and abstract methods in a simple way.