Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
if i override method with new keyword then it hide the child class function and call parent class funtion

then what's the need of "new" in overriding method. it's better to don't override method in child class.

i mean what's the use of "new"

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            child c = new child();
            Response.Write(c.func1() + "<br>" + c.func2());
        }
    }
    public class parent
    {
        public virtual string func1()
        {
            return "parent Class function 1.";
        }
        public virtual string func2()
        {
            return "parent Class function 2.";
        }

    }
    public class child : parent
    {
        public new string func1()
        {
            return "child Class function 1.";
        }
        public override string func2()
        {
            return "child Class function 2.";
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jun-12 23:32pm    
You never override anything with "new"; it is used just to silence the warning, nothing else.
The question is incorrect in principle. You cannot really ask "what is the difference between {0} and {1}?".
--SA

override: To be able to override, the method (or property) to be overriden in the base class (the class that the class is derived from) must be marked as virtual. Once a method is overridden, that method will be used whether to instantiation is cast (which is to say that when the class is viewed as either the base class or derived class) as this derived class or the base class. A good example is to look at ToString(). ToString() is a method that is defined in the object. You can see that overriding the ToString() will result in the ToString method of any class will be used instead of the object.ToString().

new: new can be used on any method (or property) in a derived class. However, if the class is cast as the base class, the method in the base class will be executed, not the method in the derived class. Only if the class is viewed as the derived class (or a class derived from the derived class) will "new" method be executed.
 
Share this answer
 
Override makes sure the derived class method is always called. With new you hide the original base class method and declare a new one that won't be called if the derived class is casted to the base class and the method is called. With override you ensure that calling the method will call the latest version of the class in the chain and when new is used you break this behaviour.
http://www.akadia.com/services/dotnet_polymorphism.html[^]

Good luck!
 
Share this answer
 
v2
Comments
BillW33 22-Jun-12 13:02pm    
Good answer, +5.
chhatrapati sharma 22-Jun-12 13:30pm    
thanks
Override: Indicate you are overriding the base class method, meaning you are making base class method obselete and intentionally adding new functionality to it.

new: new indicate you are providing new definition to it, that recreating the other definition, it says the base and derived class method are different in context and the dervied has new definition to it
 
Share this answer
 

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