Skip to main content

Posts

Showing posts from February, 2014

Method Overriding with C#

Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance. Let's understand method overriding using few examples. Output Above code consists of base class A and a derived class B. Class A consists of method Show(). Class B hides the function Show() it inherited from the base class A by providing its on implementation of Show(). In the above code we first created object of type A. Using the reference variable “a” we invoke the function Show(). As expected,  Show() of class A is executed because the reference variable “a” refers to the object of class A. Then we created an object of Derived class B and storing its reference in the reference variable “a” of type A. Using the reference variable “a” we invoke the function Show(). Since “a” contains a reference to an object of type B we would expect the function Show() of class B to get executed. But that does no