Best way to use interface and abstract class

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

Introduction

Hi guys, here I will discuss the use of interface and abstract class with real-time examples. This is one of the terrible questions in every C# and JAVA interview. Before going to any interview 30 percent of the time we are just wasting knowing the use of interface and abstract class with real-time examples. 

Interface

  • The interface is a reference type.
  • The only declaration that is possible, can not define the interface member functions.
  • The interface only contains the abstract members.

Abstract

  • The abstract is a reference type.
  • We can define the nonabstract members only
  • The abstract contains the abstract members as well as non-abstract members.

Use of interface and abstract class with real-time examples

Suppose we have a school management product where some schools are managing their day-to-day activities. let’s say all the schools have some common functionality like GetStudentInfo, GetFacultyInfo. These functionalities are applicable to all the schools. We will use this common functionality in an abstract class because in abstract class we can use abstract as well as nonabstract class.
As this product dealings with more than one school, maybe the common functionality like GetStudentInfo, GetBookDetails, and facultyinfo business logic may differ from school to school. Suppose after a few days one school owner is coming and telling that they want to implement a parking facility for their 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;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceAndAbstractUse
{
    class Program
    {
        //abstract class declaration
        //this 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)
            {
                //
            }

           
            //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, use of interface and abstract class with real-time examples. Here I discussed how you can get benefits from interface and abstract during our application development. if you found this tutorial is helpful, then please share this article with your friends, colleagues, and within your community group. I hope this article will help you. Happy to help you 😊

Leave a Comment

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