Class Types:
1) Abstract class
2) Sealed Class
3) Static Class
4) Partial Class
-----------------------------------------
A-- Abstract
S-- Sealed
S--Static
ASS is order by name
Abstract Class
Sealed Class
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("X.F3"); }
}
class Z : Y
{
// Attempting to override F causes compiler error CS0239.
// protected override void F() { Console.WriteLine("C.F"); }
// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine("Z.F2"); }
}
Static Class
a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword
1) Abstract class
2) Sealed Class
3) Static Class
4) Partial Class
-----------------------------------------
A-- Abstract
S-- Sealed
S--Static
ASS is order by name
Keyword
|
Can be instantiated(object)
|
Can be inherited
|
Abstract
|
NO
|
YES
|
Sealed
|
YES
|
NO
|
Static
|
NO
|
NO
|
Abstract Class
- It only represents the base class and thus has be inherited for its use.
- Object can NOT be created. can NOT be instantiated.
- Abstract class and methods can NOT be sealed ( class which can not be inherited).
- Abstract methods can NOT be private.
- Abstract methods can NOT be virtual ( by default they are virtual).
- Abstract class and methods can NOT be static.
- Abstract method definition can be given only inside a Abstract class. In non abstract class no abstract method can be defined.
- Can have either abstract method or non-abstract methods.
- Abstract method do not have any implementation of methods but has to be provided the same in the derived class.
- Abstract class may contain NO abstract method at all.
- Abstract methods has to be override in derived class.
- Derived class has to implement all abstract methods of abstract class.
abstract class absClass
{
public abstract int abstractMethod(int Num1, int Num2);
}
public override int abstractMethod(int Num1, int Num2)
{
return Num1 * Num2;
}
- Restrict inheritance. can NOT be inherited. Its just to take away the inheritance feature.
- Structs are sealed in nature.
- NO restriction on object creation. sealed class can be instantiated.
- If you make a sealed a method / property that overrides a virtual method /property it will get prevented from further overriding.
- Best use of sealed class is when your sealed class has static members (How?).
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("X.F3"); }
}
class Z : Y
{
// Attempting to override F causes compiler error CS0239.
// protected override void F() { Console.WriteLine("C.F"); }
// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine("Z.F2"); }
}
Static Class
- Static class must have all methods and variables as static.
- Even in static class only static constructor can be called and NO instance constructor allowed.
- can NOT be instantiated.
- static class is Sealed(can not be inherited) by default.
- Static class can NOT be abstract class ( as it can not be inherited).
- Static class can only inherit system.object class as system.object class is the base class for every class.
- Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
- Use a static class to contain methods that are not associated with a particular object.
a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword