The difference between protected and virtual / override

I could not understand the need or purpose of “protected” when I have a “virtual / override”, can someone explain to me what I need for these two things if they are almost the same.

Edit:

Thanks to all the helpers, now I understand that "protected" is for visibility purposes only, and virtual / override is for class behavior.

+6
source share
4 answers

They are certainly not the same.

The protected modifier sets the visibility of a field or method: this member can only be accessed from a class that it is defined in or from a derived class.

The virtual modifier indicates that the method applied to can be overridden in a derived class.

These modifiers can be combined: the method can be protected and virtual.

+15
source
  • protected means private for the current class and derived classes
  • virtual means that it can be used as is, but can also be overridden in derived classes

It might be better with some code instead of what you probably already read, here is a small example that you can play with. Try deleting comments (//) and you will see that the compiler tells you that properties cannot be accessed

 [TestFixture] public class NewTest { [Test] public void WhatGetsPrinted() { A a= new B(); a.Print(); //This uses B Print method since it overrides A's // a.ProtectedProperty is not accesible here } } public class A { protected string ProtectedProperty { get; set; } private string PrivateProperty { get; set; } public virtual void Print() { Console.WriteLine("A"); } } public class B : A { public override void Print() // Since Print is marked virtual in the base class we can override it here { //base.PrivateProperty can not be accessed hhere since it is private base.ProtectedProperty = "ProtectedProperty can be accessed here since it is protected and B:A"; Console.WriteLine("B"); } } 
+7
source

I think you need to understand the above two things, because both have different goals.

protected - a type or member can only be accessed by code in the same class or structure or in a derived class.

The virtual keyword is intended to change the method, property, and resolution of its overriding in a derived class.

+2
source

It can be argued that the most important difference about virtual is that it calls the compiler when the method member is called polymorphically , which compiles the code to bind. When you call a class member from client code, where the actual type of the object is, for example, the derived class is foo , but the variable it calls is actually typed (declared) as some base class, say bar , Members declared as virtual will be bound to the implementation in the actual type of the object (or to the most derived base class of an object type that has an implementation). Members not declared as virtual will be bound to the implementation in the type declared by the variable.

a. Virtual then, if the member is declared as virtual, the implementation in the derived class will be executed, even if the variable is declared as the base type.

  public class Animal { public virtual Move() { debug.Print("Animal.Move()"); } public class Bird: Animal { public virtual override Move() { debug.Print("Bird.Move()"); } Animal x = new Bird(); x.Move(); // Will print "Bird.Move" 

B. Not virtual . If a member that is not declared as virtual , then the implementation will be selected based on the declared type of the variable in which the method is executed. Therefore, if you have a Bird object in the x variable declared as "Animal" and you call the method implemented in both classes, the compiler is bound to the implementation in the Animal class, not in Bird, although the object is really a Bird.

  public class Animal { public Move() { debug.Print("Animal.Move()"); } public class Bird: Animal { public Move() { debug.Print("Bird.Move()"); } Animal x = new Bird(); x.Move(); // Will print "Animal.Move" 
+2
source

All Articles