Sealed Classes
Any of the classes we have used so far can be inherited from. If you create a certain class and don't want anybody to derive another class from it, you…
Introduction#
Most of the classes we have worked with so far are open to being used as base classes for inheritance.
sealed class Ball
{
public int TypeOfSport;
public string Dimensions;
}If the class is marked with an access modifier, the sealed keyword can appear before or after the public keyword. Here is an example:
public sealed class Ball
{
public int TypeOfSport;
public string Dimensions;
}A class that is derived from another can also be sealed. Here is an example:
public abstract class Triangle
{
}
sealed public class Irregular : Triangle
{
}There is not much to do about a sealed class. Simply remember that no class can be derived from it. After creating the sealed class, you can use it as you see fit. Here is an example:
using System;
public abstract class Triangle
{
private double bs;
private double hgt;
public Triangle(double length = 0.00D, double height = 0.00D)
{
this.bs = length;
this.hgt = height;
}
public virtual double Area()
{
return this.bs * this.hgt / 2;
}
public void Describe(string type = "")
{
Console.WriteLine("Triangle - {0}", type);
Console.WriteLine("Base: {0}", this.bs);
Console.WriteLine("Height: {0}", this.hgt);
Console.WriteLine("Area: {0}", this.Area());
}
}
sealed public class Irregular : Triangle
{
public Irregular(double Base, double Height)
: base(Base, Height)
{
}
}
public class Exercise
{
public static int Main()
{
Irregular tri = new Irregular(42.73, 35.05);
tri.Describe("Irregular");
return 0;
}
}This would produce:
Triangle - Irregular
Base: 42.73
Height: 35.05
Area: 748.84325
Press any key to continue . . .In addition to using the sealed keyword on a class, declaring a class as static also prevents inheritance.
Sealing a Property#
If you use the sealed keyword on a class, the whole class becomes sealed. You may not want the whole class to be sealed. Sometimes, you may want only some members to be sealed. To create a sealed property, its class must be derived from another class. Here is an example of such as class created as abstract:
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
}Of course, you must derive a class from such as class. Before sealing a property, you must override it from the parent class. That is, you must marked the property in the derived class as override. Here are examples:
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
}
public class Circle : RoundShape
{
private double rad;
public override double Radius
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
public override double Diameter
{
get
{
return this.rad * 2;
}
}
}You can still create a new class that is based on Circle and you can override the Radius property in that new class. If you want, you can prevent this. To seal a property, type the sealed keyword close to the override keyword. The sealed keyword can appear before or after override. Here are examples:
using System;
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
}
public class Circle : RoundShape
{
private double rad;
public sealed override double Radius // sealed before override
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
public override sealed double Diameter // override before sealed = same thing
{
get
{
return this.rad * 2;
}
}
}
public class Exercise
{
private static int Main(string[] args)
{
RoundShape circ = new Circle();
circ.Radius = 42.86;
Console.WriteLine("Circle Characteristics");
Console.WriteLine("Radius: {0}", circ.Radius);
Console.WriteLine("Diameter: {0}", circ.Diameter);
return 0;
}
}This would produce:
Circle Characteristics
Radius: 42.86
Diameter: 85.72
Press any key to continue . . .When a member has been sealed, the child classes that derive from it cannot override that member. The following will cause an error:
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
//public abstract double Area();
}
public class Circle : RoundShape
{
private double rad;
public sealed override double Radius
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
public override sealed double Diameter
{
get
{
return this.rad * 2;
}
}
}
public sealed class Sphere : Circle
{
private double rad;
public override double Radius
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
}This would produce:
Error 1 'Sphere.Radius.get': cannot override inherited member
'Circle.Radius.get' because it is sealed C:\Exercise1\Exercise.cs 47 9 Exercise1
Error 2 'Sphere.Radius.set': cannot override inherited member
'Circle.Radius.set' because it is sealed C:\Exercise1\Exercise.cs 52 9 Exercise1Sealing a Method#
Besides the properties, you can also seal a method. The primary rule to follow is that, if you know or suspect that a different version of a method will be needed by child classes, don't seal it. When deriving a new class, you have the option of sealing it or not. Here is an example:
using System;
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
public abstract double Area();
}
public class Circle : RoundShape
{
private double rad;
public sealed override double Radius
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
public override sealed double Diameter
{
get
{
return this.rad * 2;
}
}
// Don't mark this method as "sealed" because the sphere must override it
public override double Area()
{
return this.rad * this.rad * 3.14159;
}
}
public sealed class Sphere : Circle
{
// We can now seal this method because we don't intend
// to override it down. Its class is sealed already anyway
public override sealed double Area()
{
return 4 * this.Radius * this.Radius * 3.14159;
}
}
public class Exercise
{
private static int Main(string[] args)
{
RoundShape circ = new Circle();
circ.Radius = 42.86;
Console.WriteLine("Circle Characteristics");
Console.WriteLine("Radius: {0}", circ.Radius);
Console.WriteLine("Diameter: {0}", circ.Diameter);
Console.WriteLine("Area: {0}\n", circ.Area());
Sphere sph = new Sphere();
sph.Radius = 35.75; ;
Console.WriteLine("Sphere Characteristics");
Console.WriteLine("Radius: {0}", sph.Radius);
Console.WriteLine("Diameter: {0}", sph.Diameter);
Console.WriteLine("Area: {0}", sph.Area());
return 0;
}
}This would produce:
Circle Characteristics
Radius: 42.86
Diameter: 85.72
Area: 5771.036741564
Sphere Characteristics
Radius: 35.75
Diameter: 71.5
Area: 16060.5934775
Press any key to continue . . .If you define a completely new method in a child class that wasn't in the parent, it cannot be overridden later. only a method that can be overridden:
using System;
public abstract class RoundShape
{
public virtual double Radius { get; set; }
public abstract double Diameter { get; }
public abstract double Area();
}
public class Circle : RoundShape
{
private double rad;
public sealed override double Radius
{
get
{
return this.rad;
}
set
{
this.rad = value;
}
}
public override sealed double Diameter
{
get
{
return this.rad * 2;
}
}
// Don't mark this method as "sealed" because the sphere must override it
public override double Area()
{
return this.rad * this.rad * 3.14159;
}
}
public sealed class Sphere : Circle
{
// We can now seal this method because we don't intend
// to override it down. Its class is sealed already anyway
public override sealed double Area()
{
return 4 * this.Radius * this.Radius * 3.14159;
}
// Cannot use "sealed" because this method is not inherited
public double Volume()
{
return 4 * this.Radius * this.Radius * this.Radius * 3.14159 / 4;
}
}
public class Exercise
{
private static int Main(string[] args)
{
RoundShape circ = new Circle();
circ.Radius = 42.86;
Console.WriteLine("Circle Characteristics");
Console.WriteLine("Radius: {0}", circ.Radius);
Console.WriteLine("Diameter: {0}", circ.Diameter);
Console.WriteLine("Area: {0}\n", circ.Area());
Sphere sph = new Sphere();
sph.Radius = 35.75; ;
Console.WriteLine("Sphere Characteristics");
Console.WriteLine("Radius: {0}", sph.Radius);
Console.WriteLine("Diameter: {0}", sph.Diameter);
Console.WriteLine("Area: {0}", sph.Area());
Console.WriteLine("Volume: {0}\n", sph.Volume());
return 0;
}
}This would produce:
Circle Characteristics
Radius: 42.86
Diameter: 85.72
Area: 5771.036741564
Sphere Characteristics
Radius: 35.75
Diameter: 71.5
Area: 16060.5934775
Volume: 143541.554205156
Press any key to continue . . .Home Copyright © 2010-2011 C# Key

