C#, Use Of abstract, Use Of interface

When to use abstract class and interface with real time example

when to use abstract class and interface with real time example | Interface Use

Introduction

Hi guys, here I will discuss when to use abstract class and interface in real-time. This is one of the terrible questions in every C# and JAVA interview. 

when to use abstract class and interface

Suppose we have a school application product, in that product we have some common methods like GetStudentInfo, GetFacultyInfo which is applicable for all the schools. We will take the common methods in an abstract class because in abstract class we can use abstract as well as non abstract class.
Suppose this product dealings with more than one school, then in that case GetStudentInfo, GetBookDetails and facultyinfo business logic will different for different schools. Suppose after a few days one school owner is coming and telling that we want to implement a parking facility for our school, so in that case we have to implement parking details functionality only for that school, not for all the schools. Here this is not a common functionality for all the schools. This functionality only represents a particular school. So here we will take this functionality as an interface.
 
Here, I will demonstrate this requirement using C# code, please go through the below code for learning the implementation of the whole requirement. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceAndAbstractUse
{
class Program
{
//abstract class declaration
//this method will respond to all the common requirements
public abstract class SchoolInfo
{
//non abstract method
//this method will accessible by all the classes
public void GetStudentInfo(int studentId)
{
//
}

//non abstract method
//we must have to define the below methods inside all the class, where we called the abstract class
//we can implement different business functionality as per the requirement
public abstract void GetFacultyInfo();
public abstract void GetBookDetails();
}

//we must have to define below method inside all the class, where we called the interface
public interface IParkingDetails
{
void GetParkingDeatils();
}

public class SchoolOne : SchoolInfo
{
public override void GetFacultyInfo()
{
//
}

public override void GetBookDetails()
{
//
}
}

public class SchoolTwo : SchoolInfo, IParkingDetails
{
public override void GetFacultyInfo()
{
//
}

public override void GetBookDetails()
{
//
}

//interface implemented here
public void GetParkingDeatils()
{
//
}
}

public class SchoolThree : SchoolInfo
{
public override void GetFacultyInfo()
{
//
}

public override void GetBookDetails()
{
//
}
}
static void Main(string[] args)
{
}
}
}

SUMMARY

In the above article, I discussed when to use abstract class and interface in real-time. Here we learned how we can get benefits from interface and abstract during our application development. Happy to helping you 😊

About Bidyasagar Mishra

Leave a Reply

Your email address will not be published. Required fields are marked *