Results 1 to 11 of 11

Thread: Overrides

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Overrides

    What's this Overrides deal about?

    Like what's the difference between

    Public Overrides Function Blah() as boolean

    and

    Public Function Blah() as boolean

    thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    It basically allows you to override the base class's functions. So like if you have a base class called car and that car class has a function called MakeHornHonk or somthing. Well that method is a generic one for all cars. If you create a new class that inherits from car, but you don't want your horn method to work the same as the base class, you can override the MakeHornHonk function to produce your own functionality.

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    OK so how exactly would that work?

    If i had a button click event that I created this car class in

    VB Code:
    1. Dim Camaro as New Car

    And that Car class had a HonkHorn Function

    VB Code:
    1. Public Sub HonkHorn
    2. Msgbox "BEEP!"
    3. End Sub

    But i wrote another function called HonkHorn in the same form but used the Override function

    VB Code:
    1. Public Override Sub HonkHorn
    2. Msgbox "BEEP BEEP!"
    3. End Sub

    Then when i called the class's HonkHorn Function

    VB Code:
    1. Camaro.HonkHorn

    it would say BEEP BEEP! or just BEEP!?

    hmmm.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I was looking in the msdn for some syntax, and I found this example. This explains exactly what I was trying to tell you. Take a look:
    VB Code:
    1. Class Class1
    2.    Sub Method1()
    3.       MessageBox.Show("This is a method in the base class.")
    4.    End Sub
    5.    Overridable Sub Method2()
    6.       MessageBox.Show("This is another method in the base class.")
    7.    End Sub
    8. End Class
    9.  
    10. Class Class2
    11.    Inherits Class1
    12.    Public Field2 as Integer
    13.    Overrides Sub Method2()
    14.       Messagebox.Show("This is a method in a derived class.")
    15.    End Sub
    16. End Class
    17.  
    18. Protected Sub TestInheritance()
    19.    Dim C1 As New class1()
    20.    Dim C2 As New class2()
    21.    C1.Method1() ' Calls a method in the base class.
    22.    C1.Method2() ' Calls another method from the base class.
    23.    C2.Method1() ' Calls an inherited method from the base class.
    24.    C2.Method2() ' Calls a method from the derived class.
    25. End Sub
    When you run the procedure TestInheritance, you see the following messages:

    "This is a method in the base class."
    "This is another method in the base class."
    "This is a method in the base class."
    "This is a method in a derived class."

  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Ahh i just notced another keyword..."Overridable" So i guess the question would be Does a sub/function have to be dubbed "Overridable" before you can override it?

    Hmm i'll test this out and see


    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Yah, it has to be declared as overridable in order to overide it...hmm odd.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I am completely confused by the logic of this.

    If i have Class1 with Method1
    and i have CLass2 with Method1 why not just call Class2.Method1 instead of using all this override stuff?

    Or is it when you Enherit a Class you HAVE to overrite it's Method otherwise you wouldn't be albe to create the new method with the same name?

    But in that case why not just name the new method something different and call it as you normally would instead of using override?

    pheeew..
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes Override is mainly for Inheritance. You may need to keep the same name of the method to maintain backward compatibility or to work with something you already have in place.

  9. #9
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    FYI: It's also describes one of the three pillars of an object-oriented language:

    Polymorphism

    Basically, you can have related objects that may behave differently.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Arc , You should make use of the free resource in your machine (Microsoft Visual Studio.NET Documentation) . It talks about that in very basic way !

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Arc
    I am completely confused by the logic of this.

    If i have Class1 with Method1
    and i have CLass2 with Method1 why not just call Class2.Method1 instead of using all this override stuff?

    Or is it when you Enherit a Class you HAVE to overrite it's Method otherwise you wouldn't be albe to create the new method with the same name?

    But in that case why not just name the new method something different and call it as you normally would instead of using override?

    pheeew..
    Take the image object for example. It is a base class for the bitmap object. You can pass a bitmap object as a parameter that accepts a image object because all the image objects methods and properties are available in the bitmap object. The bitmap object can override a certain method, and perform its own unique way of doing things instead of using the base classes inherited method. This allows a lot of flexiblility in your programming, because you can create a base class that does somethings and inherited classes to do them differently if you need to. If you need to extend the object or change the way it processes a certain method, you can inherit the class in a new class, have all the base classes members available to you, and modify it any way you want. It is hard for me to explain any more than the msdn documentation can. It is one of those things that if you don't embrace OO programming, you will probably never use it. If you do design your apps with OO in mind, then you will probably use it sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width