Abstract Classes and Interfaces in c#

Abstract Classes and Interfaces in c#

In the C# programming language, both abstract classes and interfaces are OOPS concepts used to define or declare a class object or prototype, which is intended to be passed to derived or subclassed prototype objects within the same class. While they fulfil multiple individual purposes within existing classes, they also have some special use case differences. Both abstract classes and interfaces are essential tools in object-oriented programming for providing abstraction features to classes, allowing C# programmers to define class methods and properties without providing a complete implementation of the base class.

Abstract Classes and Interfaces in c#

So, let’s take a closer look at both abstract classes and interfaces, both OOPS concepts, in C#.

Abstract Class method in C#.

An abstract class in C# is a user-defined, declared class that cannot be directly instantiated in a program. An abstract class is designed or declared as a base or parent class to provide concepts or features for other classes to inherit. A user-defined abstract class can contain both abstract class methods (class methods without a body) and concrete methods (class methods with a body).

Important Features of an Abstract Class in C#.

  • Cannot be instantiated – C# programmers cannot directly create new class objects for a user-defined abstract class. It acts as a blueprint or prototype for derived or subclass classes.
  • Can be abstract methods – Abstract classes are user-defined class method types in a program without implementations, which must be implemented in derived or subclasses.
  • Can be concrete methods – Abstract classes are regular user-defined class methods in a program with implementations. Whose properties can be inherited by derived or subclasses.
  • Can have fields – In a user-defined class program, an abstract class can have defined class fields, properties, and constructors declared.
  • Can have access modifiers – C# programmers can use public, protected, or private class field member access modifiers for class methods and their properties.
  • Can have instance members – In a user-defined class program, an abstract method can also be defined as class fields, properties, and static members in the class.

Example of an abstract class in a C# program.

using System;

public abstract class Course

{

// here we define an abstract class method / must be implemented by derived subclasses

public abstract void Select();

// here we define Concrete class method/ that can be inherited by derived subclasses

public void Selection()

{

Console.WriteLine(“\t Select your desire course.\n”);

}

}

public class Java: Course

{

// here we are implementing the abstract class method

public override void Select()

{

Console.WriteLine(“Java language selected.”);

}

}

public class Matlab : Course

{

// here we are implementing the abstract class method

public override void Select()

{

Console.WriteLine(“Matlab language selected.”);

}

}

public class program

{

static void Main()

{

Course java = new Java();

java.Select(); // Result is – Java language selected.

java.Selection(); // Result is – Select your desire course.

Course matlab = new Matlab();

matlab.Select(); // Result is – Matlab language selected.

matlab.Selection(); // Result is – Select your desire course.

}

}

Explaining an abstract class method example.

  • In the abstract class method program above, the Course class is defined as abstract, and it declares both an abstract method (Select()) and a concrete method (Selection()).
  • Both Java and Matlab have concrete classes that implement the Select() method. However, they inherit the Selection() method from Course without providing an implementation.

Interface class method concept in C#.

In a C# program, an interface class declares or defines an object prototype of a method and its properties, which must be implemented in a user-defined class. Unlike an abstract class interface, an interface cannot provide any class implementation. All field members of a user-defined class interface are implicitly public in nature and must be implemented by any class that inherits from the interface in the current class.

Important features of a class interface in C#.

  • Cannot be an implementation – In a C# program, class interfaces only declare or define method signatures and properties. They do not provide any specific implementation in the program.
  • Cannot have fields or constructorsC# programmers cannot define class fields, constructors, or static members in a class interface.
  • Implemented by a class – User-defined classes can implement an interface, but they must provide implementations for all class methods and properties declared in the class interface.
  • Multiple interface inheritance – A user-defined class can implement multiple class interfaces simultaneously, providing programmers with greater flexibility and composition features.
  • All members are public – In a user-defined class program, all methods and properties in an interface are implicitly public in nature and cannot have class interface access modifiers such as private or protected.

Example of a class interface in C#.

Using System;

public interface ICourse

{

//here interface method declaration/without any implementation

void Select();

void Selection();

}

public class Csharp : ICourse

{

// here we are implementing the interface class methods

public void Select()

{

Console.WriteLine(“Csharp course selected.”);

}

public void Selection()

{

Console.WriteLine(“\t Select your desire programming language.”);

}

}

public class Ruby : ICourse

{

// here we are implementing the interface class methods

public void Select()

{

Console.WriteLine(“Ruby course selected.”);

}

public void Selection()

{

Console.WriteLine(“\t Select your desire programming language.”);

}

}

public class Program

{

static void Main()

{

ICourse Csharp = new Csharp();

Csharp.Select(); // Result: Csharp course selected.

Csharp.Selection(); // Result: Select your desired programming language.

ICourse ruby ​​= new Ruby();

ruby.Select(); // Result: Ruby course selected.

ruby.Selection(); // Result: Select your desired programming language.

}

}

Explaining the class interface.

  • In this program, the ICourse class interface indicates the conditions that classes implementing this interface must follow. It represents the Select() and Selection() methods.
  • Here, both Csharp and Ruby subclasses implement the ICourse interface and provide their own implementations of the Select() and Selection() class methods.

Abstract Class and Interface Method Detail Summary.

  • Abstract Class Methods – Abstract class methods are a useful OOPs class concept for providing or implementing shared features and functionality and common class behavior for user-defined related classes. User-defined abstract classes can be declared as both abstract methods (without providing an implementation) and concrete methods (with a user-defined class implementation). Abstract class methods support class fields, constructors, and multiple class method implementations.
  • Interface methods – These indicate a prototypical nature of a user-defined class, which existing classes must implement. Interface methods defined in a class cannot provide method implementations or fields. A user-defined class can implement multiple class interfaces, but the interface declared in a class can only inherit from the properties of one abstract class.

Leave a Reply