Abstract, Virtual, Static in C#

Abstract Class
- Abstract classes are the way to achieve abstraction in C#.
- A class that is declared by using the keyword abstract is called an abstract class.
- An abstract class may or may not have abstract methods i.e. it can contain both abstract methods and non-abstract (concrete) methods. But if a class contains an abstract method then it must be declared as abstract.
- We can define all static and non-static members including properties, fields, indexes as well.
- The abstract class cannot be instantiated directly, we need to create the object for its child classes to consume an abstract class.
- Class containing abstract method cannot be instantiated. It can only be inherited.
- An abstract class can have a constructor. We would provide a constructor for an abstract class if we want to initialize certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every child’s class. This prevents duplicate code.

Abstract method
- A method which is declared abstract and has no body or any implementation is called abstract methods.
- It can be only declared inside the abstract class only. Its implementation must be provided by derived classes or sub-classes. This is achieved by overriding that method and it is mandatory.

Virtual method
- Virtual methods are used for an implementation of the type-based polymorphism.
- Virtual keyword is used to distinguish it from other types of methods.
- The class containing virtual method can be instantiated.
- Virtual methods are not compulsory to be overridden.
- A virtual method has an implementation and its derived class does not have to implement it again (but can replace the original implementation).
- There is a restriction when using a virtual modifier. You cannot use this modifier along with static or abstract or override modifiers.

Static Class
- A class that is declared by using the keyword static is called an static class.
- In static class, you are not allowed to create objects.
- A static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.
- Because there is no instance variable, you can directly access the members of a static class by using the class name itself.
- It contains only static members.
- It cannot contain Instance Constructors, However, they can contain a static constructor.
- Static classes are sealed and therefore cannot be inherited.
- They cannot inherit from any class except Object.

Static Member
- A non-static class can contain static methods, fields, properties, or events.
- The static member is always accessed by the class name, not the instance name.
- Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
- Declare static class members by using the static keyword before the return type of the member.
- You use static methods when the method does not need to access any non-static class elements such as properties, events, or methods in order to do it's job.

Comments

Popular Posts