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.
Rama TitoProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Naman GoelPrinciple Software engineerCommented:
This is valid in following scenario, if we are having a protected virtual void in base class and deriving the same class in some derived class.
class MyBaseClass
    {
         protected virtual void MyMethod()
         {
             
         }
    }

    class MyDerivedClass: MyBaseClass
    {
        protected override void MyMethod()
        {
           
        }
    }

Open in new window

käµfm³d 👽Commented:
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


Screenshot
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.
käµfm³d 👽Commented:
P.S.

If a base class labels the property or method as protected, then your child class must also label the method or property as protected.

The main place you see overridable protected methods is the "OnXXX" methods within classes that raise events. This is because you don't want external classes to raise events for a given class, but you may want a child class to be able to change the behavior of an event declared/defined by the base class. For example, if you have a TextBox, then you have a TextChanged event. If you so desired, you could change the behavior of the TextBox by inheriting it in a new class and overriding the OnTextChanged event. Let's say you wanted to show a message box any time the text changed in your new, custom box:

namespace WindowsFormsApplication1
{
    class MyNewTextBox : System.Windows.Forms.TextBox
    {
        protected override void OnTextChanged(System.EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show(this.Text);

            base.OnTextChanged(e);
        }
    }
}

Open in new window


Now, any time the user changes the text in the text box (it must be of type MyNewTextBox, and not simply TextBox), a message box will appear. I can't imagine why you would wan to do this, but you could--just by overriding the OnTextChanged method that your new child class inherited from the base class.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Navneet.Net Full Stack DeveloperCommented:
Hi!

It means there is a Method in base class whose functionality to change.

You have Shape base class with method Area
now Inherting this Base class to creating New Circle class.

So
1. If you want that Method to be access only in your inherietd class you set the Method Area in base class as Prctected.
2. Also you want to change the implementation in your Circle Class you set the Methos Area as Virtual in  base class.

So when you get to inherited class Circle you get that method Area as
protected override void Area()

and you change the implementation in your new class


Very fundamental theory of OOPS, Better if you try understand OOPS.
These are just the way C# implement OOPS concepts( here Inheritance).

Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.