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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.