Abstract Class:
Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
An abstract class means that, no object of this class can be instantiated, but can make derivations of this.
ex:
namespace abstractSample
{
//Creating an Abstract Class
abstract class absClass
{
//A Non abstract method
public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
//An abstract method, to be
//overridden in derived class
public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}
//A Child Class of absClass
class absDerived:absClass
{
static void Main(string[] args)
{
absDerived cal = new absDerived();
int added = cal.AddTwoNumbers(10,20);
int multiplied = cal.MultiplyTwoNumbers(10,20);
Console.WriteLine("Added : {0}, Multiplied : {1}", added, multiplied);
}
//using override keyword,
//implementing the abstract method
public override int MultiplyTwoNumbers(int Num1, int Num2)
{
return Num1 * Num2;
}
}
}
Sealed Class:
Classes can be declared as sealed. This is accomplished by putting the sealed keyword before the keyword class in the class definition Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited. 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
ex:
class ClassExample
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class SealedClass
{
public int Mul(int x, int y)
{
return x * y;
}
}
Partial Class :
We were declaring a class in a single file but Partial class is a feature which allows us to write class across multiple files.
The partial indicates that the parts of the class, struct, or interface can be defined in the namespace. All the parts must be used with the partial keyword. All the parts must be available at compile time to form the final type or final class. All the parts must have the same accessibility level, such as public, private, protected, and so on.
Ex:
PartialTest.cs:-
namespace PartialClass
{
public partial class MyPartialTest
{
private int a;
private int b;
public void getAnswer(int a, int b)
{
this.a = a;
this.b = b;
}
}
}
PartialTest2.cs
namespace PartialClass
{
public partial class MyPartialTest
{
public void PrintCoOrds()
{
Console.WriteLine("Integer values: {0},{1}", a, b);
Console.WriteLine("Addition: {0}", a+b);
Console.WriteLine("Mulitiply: {0}", a * b);
}
}
}
Program.cs
namespace PartialClass
{
class Program
{
static void Main(string[] args)
{
MyPartialTest pts = new MyPartialTest();
ts.getAnswer(12, 25);
ts.PrintCoOrds();
Console.Read();
}
}
}
OUTPUT:
Integer values: 12,25
Addition: 37
Mulitiply: 300