Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how does method overriding perform? and what are the role of virtual,overriding, and new key word in overriding? Please Explain?


example like--

C#
class A
    {
        public virtual  void sum()//virtual remove while using new keyword in derive class method
        {
            Console.WriteLine("This is base class method");
        }
    }
    class B:A
    {
        public override void sum()//new in place of override keyword
        {
           Console.WriteLine("This is Derive class method");
        }
    }
<pre>
Posted
Updated 10-Mar-13 4:43am
v3
Comments
OriginalGriff 10-Mar-13 7:54am    
Sorry, but I have no idea what you are talking about.
How does method overriding relate to the new keyword in any way?
Perhaps if you give us an example of what you are trying to do?
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Andreas Gieriet 10-Mar-13 11:36am    
With "new" you document the hiding is intended --> turns off the hiding warning.
Andi

1 solution

Are you talking about virtual - override pair versus hiding with new?

C#
class Base
{
    public         void A() { Console.WriteLine("Base.A"); }
    public virtual void B() { Console.WriteLine("Base.B"); } // dynamic dispatch
    public virtual void C() { Console.WriteLine("Base.C"); } // dynamic dispatch
    public         void D() { Console.WriteLine("Base.D"); }
}

class Derived : Base
{
    public new      void A() { Console.WriteLine("Derived.A"); } // no warning, hiding is intended
    public override void B() { Console.WriteLine("Derived.B"); } // dynamic dispatch
    public          void C() { Console.WriteLine("Derived.C"); } // warning: hides inherited member --> missing "override" or "new" keyword
    public          void D() { Console.WriteLine("Derived.D"); } // warning: hides inherited member --> missing "new" keyword
}


static void Main(string[] args)
{
    Base inst1 = new Base();
    Base inst2 = new Derived();
    Derived inst3 = new Derived();

    inst1.A(); // Base.A
    inst1.B(); // Base.B
    inst1.C(); // Base.C
    inst1.D(); // Base.D

    inst2.A(); // Base.A
    inst2.B(); // Derived.B --> dynamic dispatch
    inst2.C(); // Base.C    --> should be dynamic dispatch, but hides since missing "override"
    inst2.D(); // Base.D

    inst3.A(); // Derived.A
    inst3.B(); // Derived.B
    inst3.C(); // Derived.C
    inst3.D(); // Derived.D
}


Intended use:
1) virtual in the base class plus override in the derived class is the intended use.
2) non-virtual plus no re-definition in the derived class is the intended use too.

Assumed error (thus, a warning) since it hides the base class method definition:
3) virtual without derived override is assumed wrong usage.
4) non-virtual with re-definition in the derived class is assumed wrong usage.

With the keyword new you document this the intended use of 3) or 4) above.
I don't know when this usage of new is advised.

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900