Home > Article > Backend Development > Abstract Class in C#
The word abstract means a thought or an idea that does not have any physical form of its own but acts as a base for other things. The same is the concept behind the abstract class. In this article, we would be discussing Abstract Classes in C#. An abstract class is a special class in C# that cannot be instantiated, i.e. you cannot create objects of an abstract class. The purpose of an abstract class is to provide a skeletal structure for other classes to derive from. Abstract classes have no implementation of their own. They require the developers and programmers to derive from the abstract class and build upon the skeletal structure, i.e. write their implementation. An abstract class can also have abstract method declarations inside. Again, these methods cannot have any definitions.
The syntax of declaring abstract classes and methods involves placing the keyword abstract before the declaration. As simple as that.
Syntax:
abstract class ClassName { public abstract void MethodName(); }
Remember, abstract methods cannot have definitions. Thus, the abstract method declarations end with a semicolon. They can only be declared. The definitions must be provided in derived non-abstract classes.
The abstract keyword instructs the compiler that the class is a base class skeletal structure to be implemented in derived classes. If the compiler finds any class deriving the abstract base class, it checks whether all the abstract methods and properties are overridden and implemented in the derived class.
Now, you may be wondering what all this are and what the interface does. So, how is an abstract class different from an interface?
Let us understand this with an example. Let us say we run a publishing company and have hired a few editors to write columns for our magazine. Let us assume they are experts in different genres, write articles to be published on a specific day of the week, and are all hired at the same salary.
Let’s define the abstract Employee class for our editors.
Code:
abstract class EmployeeEditors { public abstract void Genre(); public abstract string PublishDay(); public int Payment() { Console.Write(“Monthly salary is {0}$.”, 1000); } }
We could easily implement the Payment method, knowing that it would be the same for all. The other two methods were declared abstract to be implemented by the derived class. One could declare a normal class for each editor deriving from the abstract class and can then implement the abstract methods to define the genres and days. Now, assume that the salary of each editor is also different. One way to change our code is to declare the Payment method as abstract. The other way would be to declare the whole EmployeeEditor as an interface and give full freedom to inheriting classes.
Public interface EmployeeEditors { public abstract void Genre(); public abstract string PublishDay(); public abstract int Payment(); }
Thus, an abstract class is a base class for other classes to build upon. Whereas an interface is a blueprint for other classes to refer to and build from scratch.
There are certain rules to follow when working with abstract classes.
Problem Statement: Let us take another real-world example. Assume that you are asked to automate the process of offer letter generation for a certain organization. The current process is completely manual and requires editing existing offer letters, which is prone to error. There are certain things that would be common for all offer letters, such as work location, work timings, company title, company branding, etc. Other things such as employee name, position, salary, joining date, etc., are specific to each offer letter.
Solution: You would design an abstract class for the purpose above. Let us see how.
Code:
ode: using System; abstract class OfferLetter { public abstract string Name(string name); public abstract string Designation(string designation); public abstract int Payment(int pay); public abstract string DOJ(string doj); public string CompanyName() { return "XYZ Corporation Pvt. Ltd."; } public string OfficeAddress() { return "512, Manhattan, NY"; } public string CompanyBranding() { return this.CompanyName() + " is a privately owned regsitered corporation operating \n under the license provided by the state of New York."; } public string Disclaimer() { return "This letter and its contents are confidential in nature and are intended only \n for the recipient."+ "\nIf you are not the correct recipient, kindly return it immediately \n to " + this.CompanyName() + " " + this.OfficeAddress() + "."; } } class PrintOfferLetter : OfferLetter { public override string Name(string name) { return name; } public override string Designation(string designation) { return designation; } public override int Payment(int pay) { return pay; } public override string DOJ(string doj) { return doj; } } public class Program { public static void Main() { PrintOfferLetter ltr = new PrintOfferLetter(); string empName = "Mr. ABC", designation = "Senior Consultant", doj = "20-02-2020"; int pay = 50000; Console.WriteLine(ltr.CompanyName() + " is very happy to extend this offer letter to \n" + ltr.Name(empName) + " at the designation of " + ltr.Designation(designation) + " with an annual pay of " + ltr.Payment(pay) + "$."); Console.WriteLine("\nYou are required to report at " + ltr.OfficeAddress() + " from " + ltr.DOJ(doj) + " (dd-mm-yyyy)."); Console.WriteLine("\n\n" + ltr.CompanyBranding()); Console.WriteLine("\n\n" + ltr.Disclaimer()); } }
Output:
In a nutshell, an abstract class is simply an incomplete or partially complete class that other classes can derive and build their logic on top of it. In this article, we have seen how an abstract class is declared, and it is working. We saw real-life examples of the usage of abstract classes and how they are different from interfaces. It is recommended to try and use abstract classes as much as possible in your code. This is a crucial practice of good programming.
The above is the detailed content of Abstract Class in C#. For more information, please follow other related articles on the PHP Chinese website!