|
-
Apr 13th, 2003, 11:17 AM
#1
Thread Starter
PowerPoster
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.

-
Apr 13th, 2003, 11:21 AM
#2
PowerPoster
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.
-
Apr 13th, 2003, 11:44 AM
#3
Thread Starter
PowerPoster
OK so how exactly would that work?
If i had a button click event that I created this car class in
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
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.

-
Apr 13th, 2003, 03:21 PM
#4
PowerPoster
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:
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
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."
-
Apr 13th, 2003, 04:08 PM
#5
Thread Starter
PowerPoster
-
Apr 13th, 2003, 04:13 PM
#6
Thread Starter
PowerPoster
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.

-
Apr 13th, 2003, 04:19 PM
#7
Thread Starter
PowerPoster
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.

-
Apr 13th, 2003, 05:18 PM
#8
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.
-
Apr 13th, 2003, 06:12 PM
#9
PowerPoster
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.
-
Apr 13th, 2003, 08:13 PM
#10
Sleep mode
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 !
-
Apr 13th, 2003, 09:11 PM
#11
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|