locked
Use Override and New Keywords RRS feed

  • Question

  • Hello to all

    It is really a lot of posts about DIFFERENCE between two keywords: new and override. But

    Someone can give me example, where he/she use it in real life? I mean, in real project.

    Maybe it can be part of some pattern...

    I just dont understand where can be use of it... in real life, not in "academic course"-like examples 

    Thanks

    Wednesday, January 19, 2011 9:55 AM

Answers

  • Ohh ... sorry. I didn't read your question carefully enough.

    For example the override is used in a simple pattern where you use events.

    When I create a new event, I have always this pattern:

    public Event EventHandler<MyEventArgsType> MyEvent;

    protected virtual void OnMyEvent(MyEventArgsType e)
    {
      // Local copy to prevent race conditions
      var handler = MyEvent;

      // raise the event
      if (handler != null)
        handler(this, e);
    }

    And when another class inherits this class and overrides it (e.g. you derive something from button and you want to have something special in the OnClick), the code will be soemthing like:

    protected override void OnMyEvent(MyEventArgsType e)
    {
      // Something before base call
      ...
      base.OnMyEvent(e);
      // Something after the base call
      ...
    }

    That is just one simple pattern.

    I have no example for the other pattern. It can reduce the code you have to write when deriving from a class where the author didn't think of how usefull it could be to override that function you want to override. But I would be carefull with that because it can easily end up with unexpected results (e.g. a sportscar not accelerating like a sportscar). I think that might be helpfull if you want to use a 3rd party library and have to derive from a given class where the original author simply missed to make it virtual. With .Net Framework I never found a situation where I wanted to / had to use such a construct.

    Did that help now?

    With kind regards,

    Konrad

    • Proposed as answer by zfima Wednesday, January 19, 2011 12:37 PM
    • Marked as answer by Cookie Luo Wednesday, January 26, 2011 9:18 AM
    Wednesday, January 19, 2011 12:33 PM

All replies

  • Hi,

    first of all you have to understand inherritance. You can have 2 classes. Just as an example: You have a class Car and a SportsCar.

    Car implements a function Accelerate. A sportscar can accelerate, too. But because it is a sports car, it has it's own implementation.

    A common pattern now is to have something like this:

    A Person can own cars. So a Person has a list of cars, which can be empty or contain cars.

    So inside you have an instance of a car. This can be a Car object but also a SportsCar object. And now we get the differences:

    1) The author of Car class directly knew: Others might want to derive from my class car so I directly make it virtual (= "I want others to override it!" message)
    class Car
    {
      public virtual void Accelerate() { .. }
    }

    So SportsCar can override it:
    class SportsCar : Car
    {
      public override void Accelerate() { ...}
    }

    So this code:
    Car myCar = new SportsCar();
    myCar.Accelerate();
    is doing:
    The compiler sees the reference to a Car, but because the call to Accelerate is calling a virtual function the runtime knows: I have to check if there is a overridden version of Accelerate() and take that implementation.
    Result: Accelerate of SportsCar is done.

    2) The Author of Car does not hink about others overriding his function. If someone overrides the method, he has to use the new keyword.:

    class Car
    {
      public void Accelerate() { .. }
    }

    So SportsCar can override it:
    class SportsCar : Car
    {
      public new void Accelerate() { ...}
    }

    But now the behaviour is different:
    Car myCar = new SportsCar();
    myCar.Accelerate();
    is just calling the Accelerate method of the Car function and no longer the Accelerate function of the SportsCar.

    Maybe you want to have a closer look at this on MSDN at http://msdn.microsoft.com/en-us/library/ms173152.aspx.

    With kind regards,

    Konrad

    Wednesday, January 19, 2011 10:50 AM
  • Hi Konrad

    Thanks for the post!

    But, in my question i dont ask explain me inheritance in .NET, a ask why people need it (using keyword "new")? Did you use it in really projects??? I think, your answer is negative. Maybe not.

    So if no one use it, why CLR engineers invent this ability? 

    Wednesday, January 19, 2011 12:07 PM
  • Ohh ... sorry. I didn't read your question carefully enough.

    For example the override is used in a simple pattern where you use events.

    When I create a new event, I have always this pattern:

    public Event EventHandler<MyEventArgsType> MyEvent;

    protected virtual void OnMyEvent(MyEventArgsType e)
    {
      // Local copy to prevent race conditions
      var handler = MyEvent;

      // raise the event
      if (handler != null)
        handler(this, e);
    }

    And when another class inherits this class and overrides it (e.g. you derive something from button and you want to have something special in the OnClick), the code will be soemthing like:

    protected override void OnMyEvent(MyEventArgsType e)
    {
      // Something before base call
      ...
      base.OnMyEvent(e);
      // Something after the base call
      ...
    }

    That is just one simple pattern.

    I have no example for the other pattern. It can reduce the code you have to write when deriving from a class where the author didn't think of how usefull it could be to override that function you want to override. But I would be carefull with that because it can easily end up with unexpected results (e.g. a sportscar not accelerating like a sportscar). I think that might be helpfull if you want to use a 3rd party library and have to derive from a given class where the original author simply missed to make it virtual. With .Net Framework I never found a situation where I wanted to / had to use such a construct.

    Did that help now?

    With kind regards,

    Konrad

    • Proposed as answer by zfima Wednesday, January 19, 2011 12:37 PM
    • Marked as answer by Cookie Luo Wednesday, January 26, 2011 9:18 AM
    Wednesday, January 19, 2011 12:33 PM