What is Polymorphism, overloading, overriding and virtual?
Miky alton
The word polymorphism means having many forms. In object oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'. Polymorphism can be static or dynamic. In static polymorphism the response to a function is determined at the compile time. In dynamic polymorphism it is decided at run time. Static Polymorphism The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. These are: • Function overloading • Operator overloading Dynamic Polymorphism C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods which are implemented by the derived class. The derived classes have more specialized functionality. Please note the following rules about abstract classes: • You cannot create an instance of an abstract class • You cannot declare an abstract method outside an abstract class • When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed. Overloading You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type. Overriding Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual and overrides . Virtual If a base class method is to be overridden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding). Note that the class member method may be overridden even if the virtual keyword is not used, but its usage makes the code more transparent & meaningful.