Different types of Classes in C# .Net - With Definition and Examples
In this article, I'll explain different types of classes in C# .Net (Microsoft Tecnologies). I'll also give examples for each of the classes. It'll be useful for beginners and a good resource for interview preparation. To have a fare understanding of classes is always good for interviews and programming as well.
Classes in C#.Net
Types of classes in C#.Net:
• Abstract Class (somtimes called a Pure Virtual Class)
• Partial Class
• Sealed Class
• Static ClassAbstract Class:
An Abstract Class means that, no object of this class can be instantiated, but can make derivation of this. It can serve the purpose of base class only as no object of this class can be created.
Abstract Class is denoted by the keyword abstract.Example:
abstract class myClass
{
public myClass()
{
// code to initialize the class…
}
abstract public void anyMethod_01();
abstract public void anyMethod_02(int anyVariable);
abstract public int anyMethod_03 (int anyvariable);
}
It is important here to note that abstract classes can have non-abstract method(s), even can have only non-abstract method(s).
abstract class myclass
{
public void nonAbstractMethod ()
{
// code…
}
}
… is perfectly alright.
An abstract class can be derived from another abstract class. In that case, in the derived class, it is optional to implement the abstract method(s) of the base class.Example:
// Base abstract class
abstract class baseClass
{
public abstract int addNumbers (int a, int b);
public abstract int multiplyNumbers(int a, int b);
}
// Derived abstract class
abstract class derivedClass:baseClass
{
// implementing addNumbers…
public override int addnumbers (int a, int b)
{
return a+b;
}
}
// Derived class from 2nd class (derivedClass)
class anyClass: derivedClass
{
// implementing multiplyNumbers of baseClass…
public override int multiplyNumbers(int a, int b)
{
return a*b;
}
}
In the above example, we only implemented addNumbers in the derived abstract class (derivedClass). The abstract method multiplyNumbers is implemented in the anyClass, which is in turn derived from derivedClass. Partial Class:
This special type of class called "Partial Class" is introduced with .Net Framework 2.0. Partial Class allows its members – method, properties, and events – to be divided into multiple source files (.cs). At compile time these files get combined into a single class.
Partial Class is denoted by the keyword partial.
Some do's and don'ts about partial class:-
• All the parts of a partial class must be prefixed with the keyword partial.
• Accessibility, signature etc. must be same in all parts of the partial class.
• You cannot sealed one part of the partial class. In that case entire class in sealed.
• If you define any part of the partial class abstract, entire class will become abstract.
• Inheritance cannot be applied to a part of partial class. If you do so, it applies to entire class. Example:
public partial class myPartialClass
{
public void firstMethod()
{
// code…
}
}
public partial class myPartialClass
{
public void secondMethod()
{
// code…
}
}Sealed Class:
A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed. It is the last class in hierarchy. To access the members of a sealed class, you must create objects of that class.
Sealed Class is denoted by the keyword sealed.Example:
sealed class mySealedClass
{
int a;
int b;
}
Class mainClass
{
public static void Main()
{
mySealedClass obj = new mySealedClass();
obj.a = 5;
obj.b = 7;
Console.WriteLine("a = {0}, b = {1}", obj.a, obj.b);
}
}Static Class:
A Static Class is one which cannot be instantiated. The keyword new cannot be used with static classes as members of such class can be called directly by using the class name itself.
Following are the main characteristics of a static class:-
• A Static Class can only have static members.
• A Static Class cannot be instantiated.
• A Static Class is sealed, so cannot be inherited.
• A Static Class cannot have a constructor (except static constructor).
Static Class is denoted by the keyword static.Example:
// static class definition…
public static class myclass
{
public static int addNumbers(int a, int b)
{
return (a + b);
}
}
// to use it, we call directly on the class…
Console.WriteLine("The addition of 5 and 7 is: " + myClass.addNumbers(5, 7));All the best... !!!
Good Works.Keep Posting