Wednesday, April 14, 2010

Abstract,Static and Sealed Classes


Abstract classes
The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. An abstract class differs from a non-abstract class in the following ways:
 • An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
 • An abstract class is permitted (but not required) to contain abstract members.
 • An abstract class cannot be sealed.

 When a non-abstract class is derived from an abstract class, the non-abstract class shall include actual  implementations of all inherited abstract members, thereby overriding those abstract members. [Example: In the following code
 abstract class A
 {
 public abstract void F();
 }
 abstract class B: A
 {
 public void G() {}
 }
 class C: B
 {
 public override void F() {
 // actual implementation of F
 }
 }
 the abstract class A introduces an abstract method F. Class B introduces an additional method G, but since it doesn’t provide an implementation of F, B shall also be declared abstract. Class C overrides F and provides  an actual implementation. Since there are no abstract members in C, C is permitted (but not required) to be  non-abstract. end example]
 If one or more parts of a partial type declaration of a class include the abstract modifier, the class is abstract. Otherwise, the class is non-abstract.

 Sealed Classes
As powerful and useful as inheritance is, sometimes you will want to prevent it. For example, you might have a class that encapsulates the initialization sequence of some specialized hardware device, such as a medical monitor. In this case, you don’t want users of your class to be able to change the way the monitor is initialized, possibly setting the device incorrectly. Whatever the reason, in C# it is easy to prevent a class from being inherited by using the keyword sealed.
To prevent a class from being inherited, precede its declaration with sealed. As you might expect, it is illegal to declare a class as both abstract and sealed because an abstract class is incomplete by itself and relies upon its derived classes to provide complete implementations.
Here is an example of a sealed class:
sealed class A {
  // ...
}

// The following class is illegal.
class B : A { // ERROR! Can't derive class A
  // ...
}
As the comments imply, it is illegal for B to inherit A because A is declared as sealed.
Sealed Methods
When an instance method includes the sealed modifier, the method is said to be a sealed method,it means a derived class can not override this method.
A sealed method is used to override an inherited virtual method with the same signature.That means the sealed modifier is always used in combination with the override modifier.
Example
Class A
{ public virtual void show()
{
}
}
Class B:A
{ public sealed override void show()
{
}
}
Any derived class of B can not further override the method show().

 Static classes
 When a class declaration includes a static modifier, the class being declared is said to be a static class.
A static class declaration is subject to the following restrictions:
 • A static class shall not include a sealed or abstract modifier. (However, since a static class cannot be instantiated or derived from, it behaves as if it were both sealed and abstract.)
 • A static class shall not include a class-base specification and cannot explicitly specify a base
 class or a list of implemented interfaces. A static class implicitly inherits from type object.
 • A static class shall not contain any operators.
 • A static class shall not have members with protected or protected internal declared
 accessibility.
 • A static class shall only contain static members. [Note: constants and nested types are
 classified as static members. end note]
Example:
using System;

    static class A
    {
        public static void aMethod()
        {
            Console.WriteLine("Static class");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A.aMethod();
        }
    }

An object of type A is not needed to call the aMethod(). The type name has been used to make the call.

The members of a static class are not implicitly made static; except for constants and nested types, member declarations that are intended to be static shall explicitly include a static modifier.

No comments:

Post a Comment