简体   繁体   中英

Difference between “protected” and “virtual/override”

I couldn't understand the need or purpose of "protected" when i have "virtual/override" could someone explain me what do i need those 2 things if they are almost the same.

Edit:

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

They are certainly not almost the same.

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

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

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

  • protected means private for current class and derived classes
  • virtual means it can be used as-is but also be overridden in derived classes

Maybe it is better with some code instead of things you have probably already read, here is a little sample you can play with. Try removing the comments (//) and you can see that the compiler tells you that the properties cannot be accessed

[TestFixture]
public class NewTest
{
    [Test]
    public void WhatGetsPrinted()
    {
        A a= new B();
        a.Print();  //This uses B's 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");
    }
}

I think you need to understand above two things properly, since both has different purpose.

protected is the type or member can only be accessed by code in the same class or struct, or in a derived class.

The virtual keyword is for modify a method, property and allow it to be overridden in a derived class.

I can be argued that the Most important distinction about virtual , is that this causes the compiler, when calling a method member polymorphically, which implementation to bind the compiled code to. When you call a member of a class from client code where the actual type of the object is, say derived class foo , but the variable it is being called on is actually typed (declared) as some base class, say bar , Members declared as virtual will bind to the implementation in the actual object type, (or to the most derived base class of the objects type that has an implementation). Members not declared as virtual will bind to the implementation in the type that the variable is declared to be.

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 a 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 . When a member which is not declared as virtual , then the implementation will be chosen based on the declared type of the variable the method is executed on. So if you have a Bird Object, in variable x declared as `Animal', and you call a method that is implemented in both classes, the compiler will bind to the implementation in the Animal class, not in Bird, even though 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM