Static class, Abstract class and Interface in C# with examples

Static Class

Static Class is a special kind of class that can't be instantiated means we can't create an instance of the static class in C# language. It can only contain static members like static methods, static constructors, static property, etc... A static class is a sealed class that can't be inherited from another class. It can only have a static constructor and an instance constructor can't be created for a static class.

Static class can be created using static ​keyword in C# and below is an example of the same.

public static class Util
{
public static bool IsProd {get;set;}

public static string GetEnvironmentName()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
return environment;
}
}


As you can see above all the methods, properties are having static keywords before data type.

The following list provides features of static class

  1. It contains only static members.
  2. An instance of the static class can't be created as we do for the normal class using a new keyword.
  3. It cannot have an Instance Constructor.

We can use the static members in static class like below

Util.GetEnvironmentName();


C# language provides many predefined static classes from its library for usage in .Net application and System.Math is one of them.

double doubleValue = -1.14;
Console.WriteLine(Math.Abs(doubleValue));
Console.WriteLine(Math.Floor(doubleValue));
Console.WriteLine(Math.Round(Math.Abs(doubleValue)));

// Output:
// 1.14
// -2
// 1


We can create a static class for any custom requirement in our application and the main usage of static class is to do prefined calculation or doing some utility for the application for example we get EnvironmentName of the application using static class.

Abstract Class

Abstract class in C# is used only to be base class of other class which means all the abstract members must be implemented by the non-abstract class.

Like static class instances of the abstract class can't be created.

The following list provides features of the Abstract class

  1. The instance of Abstract Class can't be created.
  2. Abstract class must contain at least one abstract member.
  3. Mostly used for the Inheritance as a base class.
  4. It can have one or more non-abstract or completed methods.
public abstract class Shape
{
public abstract int GetArea();
}


Above is the simple abstract class named Shape which has abstract method GetArea so any derived class which is inheriting Shape abstract class will need to implement GetArea() method like below

public class Square: Shape
{
int side;
public Square(int n)
{
side = n;
}
public override int GetArea()
{
return side * side;
}
}


Abstract class in C# is basically used to force the derived class to enforce the same hierarchies or standards.

An abstract class is very useful where various derived class requires implementation of the same kind or use common behavior or status.

Interface

Interface in C# is like a contract where we have only method signature and don't have the implementation of the methods.

C# language doesn't support multiple inheritance but one class can inherit multiple interfaces so interfaces are used for multiple inheritance.

The following list provides features of an Interface.

  1. No implementation is defined in Interface.
  2. All the members are public.
  3. Classes can inherit multiple interfaces.
  4. A class inheriting an interface must implement all the methods of the interface otherwise we will get a compile-time error.
public interface IStudent
{
int GetTotalMarks(int studentId);
}

Above is an Interface IStudent with GetTotalMarks() method. The best practice is to create an interface with letter "I" but not mandatory.

public class Student: IStudent
{
public int GetTotalMarks(int studentId)
{
var englishMarks = 70;
var mathMarks = 80;
var totalMarks = englishMarks + mathMarks;
return totalMarks;
}
}

As you can see above Student class implement IStudent interface and implements the method GetTotalMarks().

Hope you understood the above important concepts in C# Language

Share This Post

Linkedin
Fb Share
Twitter Share
Reddit Share

Support Me

Buy Me A Coffee