What is the oops concept of shadowing in C#?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p align=left><font face=Arial size=2>Hola!</font>
<p align=left> 
<p align=left><font face=Arial size=2>What is the oops concept of shadowing in C#? Is it the same as hiding, using new keyword? How do you choose between override and new, as I dont see any difference </font>(apart from the fact that only virtual methods in base class can be overridden in derived class) in the following program:
<p align=left> 
using System;
<p align=left> 
class BaseClass
{
    public virtual string overridingMethod()
    {
        return "BaseClass overridingMethod n";
    }
    public string hidingMethod()
    {
        return "BaseClass hidingMethod n";
    }
}
<p align=left> 
class DerivedClass : BaseClass
{
    public override string overridingMethod()
    {
        return "DerivedClass overridingMethod n";
    }
    public new string hidingMethod()
    {
        return "DerivedClass hidingMethod n";
    }  
}
<p align=left> 
class MainClass
{
    static void Main()
    {      
       BaseClass bc = new BaseClass();     
       Console.WriteLine(bc.overridingMethod());
    
       Console.WriteLine(bc.hidingMethod());
       
       DerivedClass dc = new DerivedClass();    
       Console.WriteLine(dc.overridingMethod());   
       Console.WriteLine(dc.hidingMethod());
     }
}

View the full article
 


Write your reply...
Back
Top