- Virtual method is a method whose behavior can be overridden in derived class.
- Virtual method allows declare a method in base class that can be redefined in each derived class.
- When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
- The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
- By default, methods are non-virtual. You cannot override a non-virtual method.
- You cannot use the virtual modifier with the static, abstract, private or override modifiers.
- Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
- It is an error to use the virtual modifier on a static property.
- A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
OOP – What is static polymorphism?
- The decision is made at compile time.
- Which method is to be called is decided at compile-time only.
- Method overloading is an example of this.
- Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
- Method overloading is a concept where a class can have more than one method with the same name and different parameters.
- Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.
OOP – What is dynamic or runtime polymorphism?
- Run-time polymorphism is achieved by method overriding.
- Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
- By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
- Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by reference variable.
- Compiler would not be aware whether the method is available for overriding the functionality or not. So compiler would not give any error at compile time. At runtime, it will be decided which method to call and if there is no method at runtime, it will give an error.
C# – Rules of Private Constructor
- private Constructor is used when you don’t want to create an object for the class.These are commonly used in classes that contain static members only.If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
- We cannot inherit from classes that are having only private constructors. The reason is suppose if you are inheriting from a base class which has only private constructors and if you create an object for the derived class then the base class constructor will be called. Because the base class contains only private constructors and due to ‘private’ access modifier it is not accessible from the derived class. So it will give you an error with a syntax as follows ‘Error 1 ’ConsoleApplication2.Program.Program()’ is inaccessible due to its protection level’
- Where do you find Private constructors are useful?
Private constructors can also be useful for: classes containing only static utility methods classes containing only constants type safe enumerations.