Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# using "new" keyword when overriding methods

Discussion in 'Scripting' started by mr malee, Nov 29, 2012.

  1. mr malee

    mr malee

    Joined:
    Sep 19, 2012
    Posts:
    25
    Code (csharp):
    1.  
    2. class BaseClass : MonoBehaviour {
    3.  
    4.     new void Start() {
    5.         Debug.Log("start in base class"); //does not print
    6.     }
    7. }
    8.  
    9. class SubClass : BaseClass {
    10.  
    11.     new void Start() {
    12.         Debug.Log("start in sub class"); //prints
    13.     }
    14. }
    15.  
    I know that I can define the Start() method as a protected virtual function for override. Just wondering if it's possible to define the same method in 2 classes with the "new" keyword?

    Thanks.
     
  2. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Here is an example I found on the net from a Microsoft MVP that made good sense here

    Code (csharp):
    1. public class A
    2. {
    3.    public virtual void One();
    4.    public void Two();
    5. }
    6.  
    7. public class B : A
    8. {
    9.    public override void One();
    10.    public new void Two();
    11. }
    12.  
    13. B b = new B();
    14. A a = b as A;
    15.  
    16. a.One(); // Calls implementation in B
    17. a.Two(); // Calls implementation in A
    18. b.One(); // Calls implementation in B
    19. b.Two(); // Calls implementation in B

    Override can only be used in very specific cases. From MSDN:
    You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

    So the 'new' keyword is needed to allow you to 'override' non-virtual and static methods.

    In my Sense, Virtual with Override is better they gives you full access over calling methods.
    Use
    Code (csharp):
    1.  base.Start()
    from child class, when you want something from Super Class, otherwise Child class is doing its work with no issues.
     
    Last edited: Nov 29, 2012
    steven82036 likes this.
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You don't need new when there is nothing to override.

    But in general I would MicroEyes way, using virtual + override, this gives you explicit control and the possibility to call into the base method too which normally is the reason why you have it and use inheritance.
    The new keyword is normally only related to so called 'smells' as in 'this code stinks' and missused to undefine previously implemented functions. Thats required if the base class wasn't yours but if the base class is yours too you should properly structure the hierarchy from the start instead of using bad practices right away :)
     
  4. mr malee

    mr malee

    Joined:
    Sep 19, 2012
    Posts:
    25
    Awesome, thanks for the help :)
     
  5. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    I'm Curious to know, just why would you be looking to override something that you have already written for that same class ?