Abstract Class vs Sealed Class

Abstract Class vs Sealed Class
Difference in Abstract vs Sealed Class

Abstract Class VS Sealed Class

 

The Abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

Abstract Classes and Class Members

Classes can be declared as abstract by putting the keyword abstract before the class definition.

Example

public abstract class A
{
    // Class members here.
}

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:

public abstract class A
{
    public abstract void DoWork(int i);
}

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:

// compile with: /target:library
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}

If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Sealed Classes and Class Members

Classes can be declared as sealed by putting the keyword sealed before the class definition.

Example:

public sealed class D
{
    // Class members here.
}

A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

A method, indexer, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration. For example:

public class D : C
{
    public sealed override void DoWork() { }
}

source: MSDN | C# Programming Guide

Advertisements

OOPS | Abstract in C# Class, Methods and Properties

How Abstraction works in C# | Classes, Methods and Properties
How Abstraction works in C# | Classes, Methods and Properties

abstract (C# Reference)

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Example

In this example, the class Square must provide an implementation of Area because it derives from ShapesClass:

 
abstract class ShapesClass
{
    abstract public int Area();
}

class Square : ShapesClass
{
    int side = 0;

    public Square(int n)
    {
        side = n;
    }
    // Area method is required to avoid
    // a compile-time error.
    public override int Area()
    {
        return side * side;
    }

    static void Main() 
    {
        Square sq = new Square(12);
        Console.WriteLine("Area of the square = {0}", sq.Area());
    }

    interface I
    {
        void M();
    }
    abstract class C : I
    {
        public abstract void M();
    }

}
// Output: Area of the square = 144

Abstract classes features:

  1. An abstract class cannot be instantiated.
  2. An abstract class may contain abstract methods and accessors.
  3. It is not possible to modify an abstract class with the sealed modifier because the two modifers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
  5. Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

Abstract methods features:

    1. An abstract method is implicitly a virtual method.
    2. Abstract method declarations are only permitted in abstract classes.
    3. Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature.

      Example

      public abstract void MyMethod();
      
    4. The implementation is provided by a method override, which is a member of a non-abstract class.
    5. It is an error to use the static or virtual modifiers in an abstract method declaration.
    6. Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
    7. It is an error to use the abstract modifier on a static property.
    8. An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
    9. An abstract class must provide implementation for all interface members.
    10. An abstract class that implements an interface might map the interface methods onto abstract methods. For example:
      interface I
      {
          void M();
      }
      abstract class C : I
      {
          public abstract void M();
      }
      

Example

In this example, the class DerivedClass is derived from an abstract class BaseClass. The abstract class contains an abstract method, AbstractMethod, and two abstract properties, X and Y.

abstract class BaseClass   // Abstract class
{
    protected int _x = 100;
    protected int _y = 150;
    public abstract void AbstractMethod();   // Abstract method
    public abstract int X    { get; }
    public abstract int Y    { get; }
}

class DerivedClass : BaseClass
{
    public override void AbstractMethod()
    {
        _x++;
        _y++;
    }

    public override int X   // overriding property
    {
        get
        {
            return _x + 10;
        }
    }

    public override int Y   // overriding property
    {
        get
        {
            return _y + 10;
        }
    }

    static void Main()
    {
        DerivedClass o = new DerivedClass();
        o.AbstractMethod();
        Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);
    }
}
// Output: x = 111, y = 161

 

In the preceding example, if you attempt to instantiate the abstract class by using a statement like this:

BaseClass bc = new BaseClass();   // Error  

You will get an error saying that the compiler cannot create an instance of the abstract class ‘BaseClass’.

Source Article: MSDN | Abstract C# Reference

Will You Marry Me?

When I Fail, I Fail HARD!

 

source: 9gag

Unable to start debugging on the web server. The server committed a protocol violation. section=responsestatusline

This error usually occurs when you have set up Visual Studio to debug an existing web application running in IIS rather than the built in ASP.NET debug web server.

IIS by default listens for web requests on port 80. In this case, another application is already listening for requests on port 80. Typically, the offending application is Skype or Apache which by default takes over listening on ports 80 and 443 when installed.

If you do development in Apache too go to httpd.conf and find for below lines:


#Listen 12.34.56.78:80
Listen 80

ServerName localhost:80

Replace the port with some other port like 81 or 8080. Restart IIS and you are good to go.

If you think Skype is culprit do the following:

Skype -> Tools -> Options -> Advanced -> Connection:

Uncheck “Use port 80 and 443 as alternatives for incoming connections”

This will free those ports from skype; again Restart IIS and you are good to go!