Day 4: Abstract Classes in C#
Hi there đ, as part of our #codingwinter2021 I decided to include a topic that could be interesting for you, the abstract classes and methods. As a drawer needs to know his pencils, we need to know the tools we can use working in c#.
So letâs get started!
The keyword we need to use to create an abstract class is abstract, some parts of the documentation define abstract classes as âincomplete and must be implemented in derived classesâ. This behavior is the antonym of the sealed keyword that enables you to prevent the inheritance of a class.
But focusing on an abstract class, this can be declared this way:
The purpose of an abstract class is to provide a common definition of a base class that multiple related classes can share. You can also define an abstract method as well, by adding keyword abstract before the return type.
The abstract methods donât have an implementation thatâs why we donât use curly braces as usual. All of this could be somehow familiar to an interface but there are some differences between them:
Additional Notes:
- It is an error to use the static or virtual modifiers in an abstract method declaration.
- An abstract class cannot be instantiated.
- An abstract class may contain abstract methods and accessors.
This is the general information about abstract classes and methods, if you want to learn more about this topic, I recommend you to follow these links:
abstract â C# Reference | Microsoft Docs
Abstract and Sealed Classes and Class Members â C# Programming Guide | Microsoft Docs
Stay safe around there! đ đ đđ đ đ