Friday, August 17, 2012

Method Hiding and Overriding in c#


Method Hiding
a. We do not required to implement the base class method with 'Virtual' keyword.
b. We need to implement the subclass method with 'new' keyword
c. If we use hiding concept, there is no relationship between the base class method and the sub class method. The subclass method just hides the base class method
d. If we create a subclass object with base class reference, the object doesnt know that there is another implementation exist in the subclass for the method


Method Overriding
a. We do required to implement the base class method with 'Virtual' keyword.
b. We need to implement the subclass method with 'override' keyword
c. If we use overrider concept, it indicates that the subclass is having override relationship with the baseclass
d. If we create a subclass object with base class reference, the object takes the implementation exist in the subclass for the method

Below are sample code
a. Having two classes with one is base class and another one is subclass

b. Creating three object with one is for base class, next one is subclass with base class referenced, last one is subclass

c. See the output

No comments: