-2

I researched the Key Word Virtual and found that it allows an object's method to be overwritten. I also looked at ICollection and learned of the two options to implement it. So, if I saw someone create an Object say:

public class Lecture:

and I had the two methods for it:

public virtual ICollection<Attendance> AttendanceList { get; set; }
public virtual Room Room { get; set; }

What could these two methods be telling me?

Serious
  • 153
  • 11
  • You need to understand the virtual keyword, used for inheritance purposes. Putting virtual on a property says if an object inherits this object then the property can be overridden in the derived class. – Rayshawn Mar 23 '14 at 20:01
  • 2
    Word of advice if you can google a definition for an answer to your question, don't post a question here on stackoverflow, others will down vote your question quickly and also your answer. – Rayshawn Mar 23 '14 at 20:05
  • I did google virutal "keyword" and saw plenty of basic explanations. However, used in the two examples, I am not sure how this applies to ICollection AttendanceLIst and the other Room Room. Do you have anything that can help in this case? Thanks – Serious Mar 23 '14 at 20:17

1 Answers1

2

"The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it" (MSDN). It allows the next class that will be inheriting from it to override this method. The code below is directly from that MSDN article.

public class Shape
{
    public const double PI = Math.PI;
    protected double x, y;
    public Shape()
    {
    }
    public Shape(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    public virtual double Area()
    {
        return x * y;
    }
}

public class Circle : Shape
{
    public Circle(double r) : base(r, 0)
    {
    }

    public override double Area()
    {
        return PI * x * x;
    }
}

Here's a simple program that should give you the idea of what "virtual" does. You should now be able to answer your own question :)

Armen Aghajanyan
  • 368
  • 3
  • 15
  • Thanks. In the case of the ICollection, is it saying that "public virtual ICollection AttendanceList { get; set; }" Is this saying that Attendance Object's method AttendanceList is now eligible to be over written? – Serious Mar 23 '14 at 20:19
  • {get; set;} are just protection levels for THAT object only. That means that anyone can get that that object and anyone can change that object; For example if I have a class with an integer with {get; set;} I can say ThatObject.Number = ThatObject.Number + 1, – Armen Aghajanyan Mar 23 '14 at 20:22
  • Armen, thanks for responding. So, in my example { public class Session : SessionBrief { public string Description { get; set; } public virtual ICollection AttendanceList { get; set; } } } The only method that can NOT be over written is Description? Is that the correct assumption? – Serious Mar 23 '14 at 20:38
  • 1
    Yes. A class that derives from Session would not be able to override it. Personally I would recommend writing {get;private set;}because when you write {get;set;} anyone outside this class can change your properties. With private set, only your class can change it. – Armen Aghajanyan Mar 23 '14 at 20:41