locked
Question on Overloading functions RRS feed

  • Question

  • User2001833234 posted

    Could someone Please explain:  I have recently come across some code that has the following.  First there is a Interface with the following function    Function Validate() As Boolean. That interface is then implemented in the an 'ABSTRACT' class like this.  Public MustOverride Overloads Function Validate() As Boolean Implements IBusinessEntity.Validate

    Question 1:  Why use Overloads in the Abstract class implementation.

    Question 2:  The Abstract class is then inherited into a class (TestClass).  In TestClass the Validate function is implemented as              follows    Public Overloads Overrides Function Validate() As Boolean.  I understand the Overrides keyword is to insure that this is the version of the Validate that you want called and not the Abstract class version but why again use the keyword Overloads?

    From what I understand you can overload a constructor of a class by simply changing the constructor signature.  Also, you overload
    Methods and Properties of a class by using the Keyword Overloads.  But why do it when your implementing an Interface method, or
    an Abstract Class method that's inherited.

    Thanks

    Dave.


    Friday, October 7, 2011 12:08 PM

Answers

  • User-952121411 posted

    But why do it when your implementing an Interface method, or
    an Abstract Class method that's inherited.

    You declare 'Overloads' in the Interface or Abstract class because you want to force the class implementing either type to have to implement all overloaded declarations. Look at the following:

    Public Interface IMath
    
      Overloads Function Add(int,int)
    
      Overloads Function Add(int,int,int)
    
    End Interface

    Simple example, but the above shows I want (2) different Add methods implemented; 1 with 2 ints and 1 with 3. I typically don't find much use for the above, but I wanted to briefly explain that there is nothing against doing it. The following links might help with some further explanation:

    VB.NET PROGRAMMING PART 5 - INTERFACES:

    http://tutorials.freeskills.com/vb-net-programming-part-5-interfaces.htm

    Overloading Methods in Visual Basic .NET:

    http://msdn.microsoft.com/en-us/library/ms973896.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 10, 2011 3:49 PM

All replies

  • User-952121411 posted

    But why do it when your implementing an Interface method, or
    an Abstract Class method that's inherited.

    You declare 'Overloads' in the Interface or Abstract class because you want to force the class implementing either type to have to implement all overloaded declarations. Look at the following:

    Public Interface IMath
    
      Overloads Function Add(int,int)
    
      Overloads Function Add(int,int,int)
    
    End Interface

    Simple example, but the above shows I want (2) different Add methods implemented; 1 with 2 ints and 1 with 3. I typically don't find much use for the above, but I wanted to briefly explain that there is nothing against doing it. The following links might help with some further explanation:

    VB.NET PROGRAMMING PART 5 - INTERFACES:

    http://tutorials.freeskills.com/vb-net-programming-part-5-interfaces.htm

    Overloading Methods in Visual Basic .NET:

    http://msdn.microsoft.com/en-us/library/ms973896.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 10, 2011 3:49 PM
  • User2001833234 posted

    atconway:  Thanks for the information.

    Tuesday, October 11, 2011 10:38 AM