Rama Tito
asked on
Protected overirde void
May i get some information on "Protected override void". May I know when to use this function and what the purpose? I am a bit confuse while try to get some information on internet.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Take the following:
Open in new window
We have a Vehicle base class which declared the Move method. The virtual modifier means we can override the base class' behavior. In the base class, I have written that a Vehicle will, by default, move 10 (meters) whenever the Move method is called. The Car class did not override the Move method, so that class gets this behavior by default.
Now, the PogoStick class does override the Move method. As a result, whenever a PogoStick's Move method is called, the PogoStick object will move only 1 (meter).
Here's an example of this behavior:
Open in new window
Now, as you can see in the screenshot, even though both objects are Vehicles, each "moves" in a different manner. This is what the combination of virtual in the base class and override in the child class grants us.
The protected modifier just changes which classes have access to that method--protected means the defining/base class and any child classes have access--no one else. protected, private, public and internal (I might be forgetting one other) are all just access modifiers--they control which other classes (if any) can access the methods or properties (or classes) which they decorate.
Usually, when you begin to type "protected override", Visual Studio's Intellisense will display which methods you can override from the base class.