none
What we are achieving by interfaces over Abstract class? RRS feed

  • Question

  • Hello,

    Actually we(me and my team members) had a debate about differences between interface and abstract classes in oops.Apart from  multiple inheritance what we are achieving by using interface.

    Decoupling is one thing.Can any one explain with a real time example. Theoretical definition is not needed.

    In real time scenario what we can achieve only thru interface and abstract class.

    Its always confusing.I have read many articles so i know the theoretical background.But in real time when we can use interface/abstract class is still a question mark?


    Coding.....................................


    • Edited by La07K Tuesday, December 17, 2019 4:52 PM
    Tuesday, December 17, 2019 4:51 PM

Answers

  • One thing that can be achieved by interfaces and not by an abstract class is multiple implementations. You can implement a given interface in several classes that inherit from different ancestors, and you can implement multiple interfaces in a single class.

    Conversely, one thing that can be done by an abstract class and not by an interface is provide implementation for some members. In other words, not all members of an abstract class need to be abstract, contrary to an interface.

    • Marked as answer by La07K Wednesday, December 18, 2019 11:53 AM
    Tuesday, December 17, 2019 4:55 PM
    Moderator
  • Actually we(me and my team members) had a debate about differences between interface and abstract classes in oops.Apart from  multiple inheritance what we are achieving by using interface.

    The ability use the new is glue principle, Inversion of Control and dependency injection, loose coupling, unit testing and the ability to mockout an object during a unit test.

    In real time scenario what we can achieve only thru interface and abstract class.

    I talk about usage of Interface and abstract class in below form link.

    But with an abstract class, you can use an abstract class to control the developer/tell the developer what must be implemented in the derived class 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/1df762e2-ddab-4947-81d0-30be968e4d56/doubt-in-openclosed-principle?forum=csharpgeneral

    It doesn't matter if talking about an ASP.NET or Windows desktop solution concerning clean code.

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    This is the startup of the Windows from Core 3.0 solution that is being talked about in the above link where the IoC called services in .NET Core is being setup so the objects can be DI into a class/object.

    using System;
    using System.Windows.Forms;
    
    namespace PubCompanyWinCore
    {
        static class Program
        {
            /// <summary>
            ///  The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                // Add handler to handle the exception raised by main threads
                Application.ThreadException +=
                    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    
                // Add handler to handle the exception raised by additional threads
                AppDomain.CurrentDomain.UnhandledException +=
                    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
                 new Startup();
    
                // Stop the application and all the threads in suspended state.
                Environment.Exit(-1);
            }
            static void Application_ThreadException
                (object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                // All exceptions thrown by the main thread are handled over this method
    
                ShowExceptionDetails(e.Exception);
            }
    
            static void CurrentDomain_UnhandledException
                (object sender, UnhandledExceptionEventArgs e)
            {
                // All exceptions thrown by additional threads are handled in this method
    
                ShowExceptionDetails(e.ExceptionObject as Exception);
            }
    
            static void ShowExceptionDetails(Exception Ex)
            {
                // Do logging of exception details
                MessageBox.Show(Ex.Message, Ex.TargetSite.ToString(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                // Stop the application and all the threads in suspended state.
                Environment.Exit(-1);
            }
        }
    }
    
    
    
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.Extensions.DependencyInjection;
    using BLL;
    using DAL;
    using DAL.Models;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    
    namespace PubCompanyWinCore
    {
        public class Startup
        {
          public Startup()
          {
                var serviceCollection = new ServiceCollection();
    
                ConfigureServices(serviceCollection, new DbContextOptionsBuilder());
    
                var _serviceProvider = serviceCollection.BuildServiceProvider();
                
                Application.SetHighDpiMode(HighDpiMode.SystemAware);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                MainView view = _serviceProvider.GetService<MainView>();
                
                Application.Run(view);
          }
           
          private void ConfigureServices(IServiceCollection services, DbContextOptionsBuilder optionsBuilder)
          {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())  //location of the exe file
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
                IConfigurationRoot configuration = builder.Build();
    
                var connectionstring = configuration.GetConnectionString("DefaultConnection");
    
                //BLL
                services.AddTransient<IPayRollDM, PayRollDM>();
                services.AddTransient<IAuthorDM, AuthorDM>();
    
                //DAL
                services.AddTransient<IDaoAuthor, DaoAuthor>();
                services.AddTransient<IDaoPayroll, DaoPayroll>();
                services.AddTransient<IDaoArticle, DaoArticle>();
                services.AddDbContext<PublishingCompanyContext>(options => options.UseSqlServer(connectionstring));
    
                //Presentation
                services.AddSingleton<MainView>();
                services.AddSingleton<PayRollView>();
                services.AddSingleton<AuthorView>();
          }  
        }
    }
    



    Tuesday, December 17, 2019 5:43 PM

All replies

  • One thing that can be achieved by interfaces and not by an abstract class is multiple implementations. You can implement a given interface in several classes that inherit from different ancestors, and you can implement multiple interfaces in a single class.

    Conversely, one thing that can be done by an abstract class and not by an interface is provide implementation for some members. In other words, not all members of an abstract class need to be abstract, contrary to an interface.

    • Marked as answer by La07K Wednesday, December 18, 2019 11:53 AM
    Tuesday, December 17, 2019 4:55 PM
    Moderator
  • Actually we(me and my team members) had a debate about differences between interface and abstract classes in oops.Apart from  multiple inheritance what we are achieving by using interface.

    The ability use the new is glue principle, Inversion of Control and dependency injection, loose coupling, unit testing and the ability to mockout an object during a unit test.

    In real time scenario what we can achieve only thru interface and abstract class.

    I talk about usage of Interface and abstract class in below form link.

    But with an abstract class, you can use an abstract class to control the developer/tell the developer what must be implemented in the derived class 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/1df762e2-ddab-4947-81d0-30be968e4d56/doubt-in-openclosed-principle?forum=csharpgeneral

    It doesn't matter if talking about an ASP.NET or Windows desktop solution concerning clean code.

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    This is the startup of the Windows from Core 3.0 solution that is being talked about in the above link where the IoC called services in .NET Core is being setup so the objects can be DI into a class/object.

    using System;
    using System.Windows.Forms;
    
    namespace PubCompanyWinCore
    {
        static class Program
        {
            /// <summary>
            ///  The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                // Add handler to handle the exception raised by main threads
                Application.ThreadException +=
                    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    
                // Add handler to handle the exception raised by additional threads
                AppDomain.CurrentDomain.UnhandledException +=
                    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
                 new Startup();
    
                // Stop the application and all the threads in suspended state.
                Environment.Exit(-1);
            }
            static void Application_ThreadException
                (object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                // All exceptions thrown by the main thread are handled over this method
    
                ShowExceptionDetails(e.Exception);
            }
    
            static void CurrentDomain_UnhandledException
                (object sender, UnhandledExceptionEventArgs e)
            {
                // All exceptions thrown by additional threads are handled in this method
    
                ShowExceptionDetails(e.ExceptionObject as Exception);
            }
    
            static void ShowExceptionDetails(Exception Ex)
            {
                // Do logging of exception details
                MessageBox.Show(Ex.Message, Ex.TargetSite.ToString(),
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                // Stop the application and all the threads in suspended state.
                Environment.Exit(-1);
            }
        }
    }
    
    
    
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.Extensions.DependencyInjection;
    using BLL;
    using DAL;
    using DAL.Models;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    
    namespace PubCompanyWinCore
    {
        public class Startup
        {
          public Startup()
          {
                var serviceCollection = new ServiceCollection();
    
                ConfigureServices(serviceCollection, new DbContextOptionsBuilder());
    
                var _serviceProvider = serviceCollection.BuildServiceProvider();
                
                Application.SetHighDpiMode(HighDpiMode.SystemAware);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                MainView view = _serviceProvider.GetService<MainView>();
                
                Application.Run(view);
          }
           
          private void ConfigureServices(IServiceCollection services, DbContextOptionsBuilder optionsBuilder)
          {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())  //location of the exe file
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
                IConfigurationRoot configuration = builder.Build();
    
                var connectionstring = configuration.GetConnectionString("DefaultConnection");
    
                //BLL
                services.AddTransient<IPayRollDM, PayRollDM>();
                services.AddTransient<IAuthorDM, AuthorDM>();
    
                //DAL
                services.AddTransient<IDaoAuthor, DaoAuthor>();
                services.AddTransient<IDaoPayroll, DaoPayroll>();
                services.AddTransient<IDaoArticle, DaoArticle>();
                services.AddDbContext<PublishingCompanyContext>(options => options.UseSqlServer(connectionstring));
    
                //Presentation
                services.AddSingleton<MainView>();
                services.AddSingleton<PayRollView>();
                services.AddSingleton<AuthorView>();
          }  
        }
    }
    



    Tuesday, December 17, 2019 5:43 PM
  • Thanks Alberto:)

    Coding.....................................

    Wednesday, December 18, 2019 11:53 AM