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!
:)
Printable View
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!
:)
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.
OK so how exactly would that work?
If i had a button click event that I created this car class in
VB Code:
Dim Camaro as New Car
And that Car class had a HonkHorn Function
VB Code:
Public Sub HonkHorn Msgbox "BEEP!" End Sub
But i wrote another function called HonkHorn in the same form but used the Override function
VB Code:
Public Override Sub HonkHorn Msgbox "BEEP BEEP!" End Sub
Then when i called the class's HonkHorn Function
VB Code:
Camaro.HonkHorn
it would say BEEP BEEP! or just BEEP!?
hmmm.
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:
When you run the procedure TestInheritance, you see the following messages:VB Code:
Class Class1 Sub Method1() MessageBox.Show("This is a method in the base class.") End Sub Overridable Sub Method2() MessageBox.Show("This is another method in the base class.") End Sub End Class Class Class2 Inherits Class1 Public Field2 as Integer Overrides Sub Method2() Messagebox.Show("This is a method in a derived class.") End Sub End Class Protected Sub TestInheritance() Dim C1 As New class1() Dim C2 As New class2() C1.Method1() ' Calls a method in the base class. C1.Method2() ' Calls another method from the base class. C2.Method1() ' Calls an inherited method from the base class. C2.Method2() ' Calls a method from the derived class. End Sub
"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."
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!:D
Yah, it has to be declared as overridable in order to overide it...hmm odd.
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..:confused:
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.
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.
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 !;)
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.Quote:
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..:confused: