Link to home
Start Free TrialLog in
Avatar of Rama Tito
Rama TitoFlag for Malaysia

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
Avatar of Naman Goel
Naman Goel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As indicated by naman_goel, you would use this when you are inheriting a base class. Within that base class, you have method that is declared as virtual or abstract (ignore the "protected" for right now). If, in the case of virtual, you want to change what that method does in the base class, then you would override the method. When you override the method, you may choose to also call the base class' version also, but you don't have to.

Take the following:

class Vehicle
{
    protected int distanceTraveled = 0;

    public virtual void Move()
    {
        this.distanceTraveled += 10; // Move 10 meters
    }

    public override string ToString()
    {
        return this.GetType().Name + " has travelled " + this.distanceTraveled.ToString() + " meters.";
    }
}

class Car : Vehicle
{
    // I inherit Vehicle and so I automatically
    //  get a Move() method, but I accept the
    //  default behavior of Move() because I
    //  didn't override it.
}

class PogoStick : Vehicle
{
    public override void Move()
    {
        // I override the base class' Move()
        //   method because I only want to
        //   move 1 meter when my version of
        //   Move() is called.
        this.distanceTraveled += 1; // Move 1 meter
    }
}

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:

static void Main(string[] args)
{
    Vehicle car = new Car();
    Vehicle pogo = new PogoStick();

    Console.WriteLine("*******************************");
    Console.WriteLine("**      Let's move a car     **");
    Console.WriteLine("*******************************");

    Console.WriteLine(car);
    car.Move();
    Console.WriteLine(car);
    car.Move();
    Console.WriteLine(car);

    Console.WriteLine("\n");
    Console.WriteLine("*******************************");
    Console.WriteLine("**  Let's move a pogo stick  **");
    Console.WriteLine("*******************************");

    Console.WriteLine(pogo);
    pogo.Move();
    Console.WriteLine(pogo);
    pogo.Move();
    Console.WriteLine(pogo);

    Console.ReadKey();
}

Open in new window


User generated image
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial