Classes can be declared as sealed. This is accomplished by
putting the sealed keyword before the keyword class in the class definition.
For example:
Listing 9
public sealed class classSealed
{
// Class members here.
public string ID;
public double Price;
}
In the following code listing the implementation of a sealed
class has been tested.
Listing 10
classSealed sc=new classSealed();
sc.ID=”C1”;
sc.Price=500.00;
double Samt=sc. CalculatePrice();
Response.Write(“Total Net Price of Product : “ +” “+sc.ID +” “+ “is”+” “+Samt);
Response.Write(“<br>”);
A sealed class cannot be used as a base class. For this
reason, it cannot also be an abstract class. Sealed classes are primarily used
to prevent derivation. Because they can never be used as a base class, some
run-time optimizations can make calling sealed class members slightly faster.
Sealing a class means one can not derive from it. Sealing a method means
one can not override it. In C# structs are implicitly sealed; therefore, they
cannot be inherited. If we try to inherit from a sealed class in another class we
will get compile time error about Inconsistent accessibility (code is shown in
following code listing).
Listing 11
public class TestClass : classSealed
{
}
In C# a method can not be declared as sealed. However when
we override a method in a derived class, we can declare the overridden method
as sealed as shown below. By declaring it as sealed, we can avoid further
overriding of this method.
Listing 12
public class testClass
{
public int x;
public int y;
public virtual void testMethod(){
}
}
public class TestClass: testClass
{
public override sealed void testMethod(){
}
}
So Which One Should I Use?
Abstract classes can add more functionality without
destroying the child classes that were using the old version. Abstract classes
provide a simple and easy way to version our components. By updating the base
class, all inheriting classes are automatically updated with the change. In an interface,
creation of additional functions will have an effect on its child classes due
to the necessary implementation of interface Methods in classes. Abstract
classes should be used primarily for objects that are closely related, whereas
interfaces are best suited for providing common functionality to unrelated
classes. Say there are two classes, bird and airplane, and both of them have methods
called fly. It would be ridiculous for an airplane to inherit from the bird
class just because it has the fly() method. Rather, the fly() method should be
defined as an interface and both bird and airplane should implement that
interface. If we want to provide common, implemented functionality among all
implementations of component, we should use an abstract class. Abstract classes
allow us to partially implement classes, whereas interfaces contain no
implementation for any members. So the selection of interface or abstract classes
depends on the needs and design of our project. We can make an abstract class, interface,
or combination of both depending on our needs.
The most likely situation in which we make a class or method
sealed will be if the class or method is internal to the operation of the library,
class, or other classes that we are writing. Because any attempt to override
some of its functionality will cause problems. We can mark a class or method as
sealed for commercial reasons, in order to prevent a third party from extending
our classes. For example, in the .NET base class library string is a sealed
class.
We should not use the sealed key word on a method unless
that method is itself an override of another method in some base class. If we
are defining a new method and we don’t want anyone else to override it, we
should not declare it as virtual in the first place. If however, we have
overridden a base class method, the sealed keyword provides a way of ensuring
that the override supplied to a method is a “final” override that means no one else
can override it again.