Sealing and Hiding: Take Two

As previously mentioned, the sealed keyword prevents a class from being derived any further. We can also apply such specialization prevention at a more granular level of members, too. For example, although we may leave our Square type unsealed (the default), we can prevent people from overriding the virtual properties we’ve defined:

class Square : Shape{    public Square(double side)    {        Side = side;    }    public double Side { get; private set; }    public sealed override double Area    {        get { return Side * Side; }    }    public sealed override double Circumference    {        get { return 4 * Side; }    }}

Now if someone comes in and defines a subclass of Square, say ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.