none
Java method.Super() keyword in C#

    Question

  • I tried to use super() is a class but it turned an error.  What method in C# adds a method from a superclass into a subclass? 

    Also, If I have in my superClass:

    public int superClassMethod(int x) {

    return x

    }

     

    and in my subclass:

    public int superClassMethod(int x) {

    super(x);

    //class specific code

    }

     

    Class specific code won't even run, because super is part of the superclass's method.  I can't get rid of the return in the superclass because I'll have an error and the code won't return anything. 

    How can I have methods in by subclass that override superclass methods, but also inherit all their great code (but also allow me to add specialized meaning in the sublcass)?!?

     

     

     

    Thursday, February 08, 2007 11:57 AM

All replies

  • the equivalent of super in C# is base
    here is an example

    protected override void OnPaint(PaintArgs e){

       base.OnPaint(e); // calls the OnPaint of the base class (super class) 
    }

    • Proposed as answer by aoneal Sunday, April 20, 2014 5:46 PM
    • Unproposed as answer by aoneal Sunday, April 20, 2014 5:46 PM
    Thursday, February 08, 2007 12:18 PM
  • If I may add a bit onto this please?  Java (by default) allows for any methods to be overridden, C# on the other hand does not.  The reason IMHO is more C/C++ based than MS cares to admit.  They say you should explicitly declare your intentions to have a method be overridable from the get go.  If you do not, then the compiler will complain and say you need to use the 'new' keyword for that other method in the subclass to shadow (or hide) the base implementation.  I'm not saying whether I agree or disagree, just I know what you're dealing with (yup, I been there! :>).

     

    To answer your question, you need to tell your base class "it can be overridden in the future" with the "virtual" keyword, then in the class overridding it, you need two pieces, one telling the compiler to "override" the virtual method (as seen in Z.Y.S posting), and then the base.SomeMethodCall(int SomeArgument).  You don't have to have both pieces, just that you need them to do what you want to do (chain your method calls through the class hierarchy method stack, and take advantage of OOP! :>>>>)

     

    Something else to look into (which I'm sure you've already thought of), both java and C# have the concept of constructor chaining, but their implementation is slightly different.  The base method call is after the constructor arguments but before the opening '{'.  Just a teaser to peak your interest a bit. :>

     

    Good luck.

    Thursday, February 08, 2007 2:28 PM
  • Okay, so base = super. 

    And not all methods can be overriden. 

     

    But if I want to override a method, how do I get around the problem of there being a return at the end of base.method which prevents the added on code from running?


     

    Thursday, February 08, 2007 8:03 PM