Introduction
Data abstraction is the process of presenting the user only the information that is necessary and that they wish to see while obscuring other information.
What is an abstract class?
Abstract classes or interfaces can be used to accomplish abstraction.
An abstract class serves as a base class for additional classes and is a unique kind of class that cannot be created.
Members of an abstract class that are designated as such must be implemented by derived classes.
A base class might be an abstract class, and the definitions of the abstract class should be implemented by all other derived classes.
Using the keyword override, all non-abstract classes should implement all abstract methods.
The abstract method is in the non-abstract class after overriding.
This class can be derived from another class, and finally, we can use it to override the same abstract method.
An abstract class is meant to provide both common functionality that may be overridden by various derived classes and basic functionality that can be shared by all of them.
Syntax of an Abstract Class
Let's examine the syntax of an abstract class in C# that has the declared abstract method "Calculate()" as its only method. Derived classes can supply its implementation if needed.
public abstract class class_name
{
}
Example
public abstract class Shap
{
}
Abstract methods
As a general rule, a method that is declared as abstract has no "body" and can only be declared inside the abstract class.
All non-abstract classes should implement an abstract method using the "override" keyword.
The abstract method is in the non-abstract class context after overriding the methods.
Syntax of an Abstract Methods
public abstract return_type method_name([parameter1, parameter2]);
Example
public abstract double Calculate(double a, double b);
Implementation of an abstract class
public abstract class Volumne
{
public abstract double CalculateCube(double a);
public abstract double CalculateCuboid(double l, double b, double h);
private double CalculatePI()
{
return 22 / 7.0;
}
public double CalculateCylinder(double r, double h)
{
return CalculatePI() * Math.Pow(r, 2) * h;
}
}
Is it possible to make an abstract class instance in C#?
No. An abstract class cannot have an instance created for it. It is impossible to create an instance of an abstract class, regardless of whether it has any abstract methods or not. If you attempt, a compile-time error will appear, as seen in the picture below.
Assume the Volume class mentioned above has a child class. Subsequently, the child class has to implement the abstract methods CalculateCube and CalculateCuboid. Please pay attention to the code below. In this instance, the Shape class that derives from the Volume class has been constructed. The two abstract methods have not been used in this instance. We are thus receiving a compile-time error.
We are receiving two errors here. Errors for failing to implement the parent class CalculateCube method and the parent class CalculateCuboid method were the two main ones. This implies that all of the parent class's abstract methods must have an implementation provided by the child class.
Why is it Unable to instantiate an Abstract Class in C#?
It is not a completely developed class, therefore its abstract methods cannot be used. We can use an abstract object to invoke an abstract method that the CLR cannot execute at runtime if the compiler permits us to generate the object for an abstract class. Therefore, the compiler prevents us from instantiating an abstract class in order to limit calling abstract methods.
Let's now put the two abstract methods into practice inside the child class. The override modifier must be used in the following manner to implement the abstract methods.
public class Shape : Volumne
{
public override double CalculateCube(double a)
{
return a * a * a;
}
public override double CalculateCuboid(double l, double b, double h)
{
return l * b * h;
}
}
As you can see, the compile-time error has disappeared. The child class can now use the parent class's non-abstract methods since it has now implemented the abstract methods that were necessary to meet the parent class's criteria. Consequently, you can now instantiate the Child class and use every member in the manner described below.
Shape shape = new Shape();
var result = shape.CalculateCylinder(10, 3.4);
Console.WriteLine("shape.CalculateCylinder(10, 3.4): {0}", result);
Console.WriteLine();
result = shape.CalculateCube(7);
Console.WriteLine("shape.CalculateCube(3): {0}", result);
Console.WriteLine();
result = shape.CalculateCuboid(10, 3.4, 7.5);
Console.WriteLine("shape.CalculateCuboid(10, 3.4, 7.5): {0}", result);
Console.WriteLine();
Qualities of the Abstract Class
It is impossible to instantiate an abstract class.
Both abstract and non-abstract members can be found in an abstract class.
Given that the abstract modifier necessitates inheritance and the sealed modifier forbids it, an abstract class cannot be a sealed class.
All of the abstract members of the parent abstract class must have actual implementations in every non-abstract class that derives from it.
It is possible to inherit an abstract class from a class, an interface, or both.
Access modifiers such as internal, protected, and private can be applied to class members of an abstract class. However, a private access modifier cannot be applied to abstract members.
Instance variables (such as fields and constants) are allowed in abstract classes.
Constructors and destructors are possible for an abstract class.
A virtual method is implicitly a method that is abstract.
Abstract methods function similarly to abstract characteristics.
Structures cannot inherit an abstract class.
Multiple inheritance is not supported by an abstract class.
Standardized design principles for Abstract classes
In an abstract class, public constructors should not be defined. Constructors with public access modifiers make the classes that can be instantiated visible, whereas abstract classes cannot be instantiated.
Within an abstract class, define an internal or protected constructor. An internal constructor can be used to restrict concrete implementations of the abstract class to the assembly that contains it, and a protected constructor enables the base class to initialize itself when subclasses are generated.
Using abstract classes in C# has a number of benefits and drawbacks. These are a few of the important ones:
Benefits
- Encapsulation: By defining a common set of behaviors or traits that derived classes must possess, abstract classes shield the inner workings of their implementation from external scrutiny. You can write more flexible and maintainable code by doing this.
- Code reuse: By serving as a foundation class for several derived classes, abstract classes can minimize code duplication and increase code reuse.
- Polymorphism: You can build code that interacts with objects of different derived classes as long as they all inherit from the same abstract base class by using abstract classes to achieve polymorphism.
Drawbacks
- Tight coupling: It may be more difficult to change the base class without also changing the derived classes when there is a tight coupling between the base class and the derived classes caused by abstract classes.
- Restricted inheritance: An abstract class used as a base class restricts the ability of derived classes to inherit from other classes since C# only permits a class to inherit from a single base class.
- Testing difficulty: Abstract classes can be more challenging to test than normal classes since they cannot be instantiated directly. You might need to make a mock or stub of the abstract base class before you can test a derived class.
Summary
The ideas of abstraction pertaining to methods and classes are an extension of the inheritance we have been studying. Using a parent class, we can give a child class property that the child classes can use. This allows us to reuse code while maintaining security and accessibility. I hope you now understand every aspect of the abstract class.
FAQs
Q 1. How is an abstract class used in C#?
To serve as a guide for derived classes and to provide requirements that derived classes must follow in order to inherit an abstract class.
Q 2. Why do we use an abstract class?
To serve as a foundation for subclasses to implement and extend the abstract methods, as well as to use or override the implemented methods within the abstract class.
Q 3. Why is an abstract class necessary?
In order to give derived classes a blueprint or skeleton framework.
Q 4. What distinguishes a C# interface from an abstract class?
Static members are found in abstract classes. There are no static members in the interface.
We learned the new technique and evolved together.
Happy coding!