The purpose of abstract class is to provide default functionality to its sub classes.
When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.
An abstract class can also contain methods with complete implementation, besides abstract methods.
When a class contains at least one abstract method, then the class must be declared as abstract class.
When a class contains at least one abstract method, then the class must be declared as abstract class.
It is mandatory to override abstract method in the derived class.
When a class is declared as abstract class, then it is not possible to create an instance for that class.
They are commonly used when you want to define a template for a group of subclasses that share some common implementation code, but you also want to guarantee that the objects of the superclass cannot be created.
Use abstract classes when you are defining behaviour for a class in your class heirarchy that is never going to be used to instantiate an object directly.
Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.
Compared to an interface, it adds features and improves performance.
Features of Abstract Class
- An abstract class cannot be instantiated.
- An abstract class contain abstract members as well as non-abstract members.
- An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
- A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
- An abstract class can be inherited from a class and one or more interfaces.
- An Abstract class can has access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
- An Abstract class can has instance variables (like constants and fields).
- An abstract class can has constructors and destructor.
- An abstract method is implicitly a virtual method.
- Abstract properties behave like abstract methods.
- An abstract class cannot be inherited by structures.
- An abstract class cannot support multiple inheritance
Reference Link :
No comments:
Post a Comment