Arjun Singh
When we have to use Interface and Abstract practically?
By Arjun Singh in .NET on Jun 23 2018
  • Kalyani Shevale
    Jun, 2018 26

    Since multiple inheritance is not supported in C#, you cannot inherit your class from two abstract classes. An interface is your only option in such situations. If there is no default or common behaviour among all the classes that are inheriting from abstract class then interface may be a better choice.

    • 12
  • Jignesh Kumar
    Jul, 2018 27

    Interface : If you want to implement multiple inheritance in C# then there is no other choice you have to go for interface. second scenario when you want to force developer to implement all your method and you do not have any default implementation for your derived class that time you can go for interface. Abstract class : If you have some default implemented method which all derived class will use so you can provide in your base abstract class which will be used by child class. one more thing about abstract class that developer can't create instance of abstract class.

    • 7
  • Arjun Singh
    Jun, 2018 23

    Not mentioning any comparison here but practically..When there is a need to enforce to implement some logic by all classes having its own set of definitions and also implementing interface based programming then go for interface. However, if there is a requirement to keep some common logic for all derived classes along with enforcing to implement its own definition for some logic then go for Abstract. Keeping in mind if an abstract class has all members as an abstract then it behaves like an interface.

    • 5
  • Yogesh Ghorpade
    Jul, 2018 10

    The abstract class is a class that is declared abstract it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static fields and static methods. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract

    • 4
  • Shrimant Telgave
    Jul, 2018 4

    We will create small console project to understand when to use Abstract class and Interface as follow.Let's consider we need to create one small application which will draw the shape and clacualteArea of shape and calculate the diameter of the shape.The following code will achieve the when to use the abstract class.namespace WhenToUseAbstractClassAndInterface {public abstract class Shape{public string ShapeName { get; set; }public void DrawShape(){Console.WriteLine("Draw shape is "+this.ShapeName);}public abstract double CalculateArea();}public class Rectangle:Shape{public double Width { get; set; }public double Height { get; set; }public override double CalculateArea(){return this.Width * this.Height;}}public class Circle:Shape{public double PI { get; set; }public double Radius { get; set; }public override double CalculateArea(){return this.PI *(this.Radius*this.Radius);}}class Program{static void Main(string[] args){Rectangle rect = new Rectangle(){ShapeName="Rectangle",Width=10.10,Height=12.1};rect.DrawShape();Console.WriteLine("Area of Rectangle is {0}", rect.CalculateArea());Circle circle = new Circle(){ShapeName = "Circle",PI = 3.14,Radius=10.1};circle.DrawShape();Console.WriteLine("Area of Circle is {0}", circle.CalculateArea()); Console.ReadLine();}} }So we have come new enhancement in the above application. we want to implement the calculate the Diameter of a circle. So to implement this functionality we need to declare the separate interface. and inherits in circle class.Why because the calculating diameter is only required for circle class, not for other classes as follow.Create the interface for calculating diameter as followinterface ICircle{double CalculateDiameter();}Inherits to the Circle class only. Full application code with abstract class and interface as follow.Full application code when to use abstract class and interface in c#.using System; namespace WhenToUseAbstractClassAndInterface {interface ICircle{double CalculateDiameter();}public abstract class Shape{public string ShapeName { get; set; }public void DrawShape(){Console.WriteLine("Draw shape is "+this.ShapeName);}public abstract double CalculateArea();}public class Rectangle:Shape{public double Width { get; set; }public double Height { get; set; }public override double CalculateArea(){return this.Width * this.Height;}}public class Circle:Shape,ICircle{public double PI { get; set; }public double Radius { get; set; }public double CalculateDiameter(){return 2 * (this.Radius);}public override double CalculateArea(){return this.PI *(this.Radius*this.Radius);}}class Program{static void Main(string[] args){Rectangle rect = new Rectangle(){ShapeName="Rectangle",Width=10.10,Height=12.1};rect.DrawShape();Console.WriteLine("Area of Rectangle is {0}", rect.CalculateArea());Circle circle = new Circle(){ShapeName = "Circle",PI = 3.14,Radius=10.1};circle.DrawShape();Console.WriteLine("Area of Circle is {0}", circle.CalculateArea());Console.WriteLine("Diameter of Circle is {0}", circle.CalculateDiameter()); Console.ReadLine();}} }I hope this will help you to understand when to use abstract class and when to use interface.

    • 4
  • Shivam Shukla
    Jun, 2018 27

    The interface is used when you want to define a contract and you don't know anything about implementation.An abstract class is used when you know something and rely on others for what you don't know. It is partial abstraction as some of the things you may know and some you don't.

    • 4
  • Arslan Zulfiqar
    Jul, 2018 18

    If you are creating something that provides common functionality to unrelated classes, use an interface. If you are creating something for objects that are closely related in a hierarchy, use an abstract class. An example of this would be something like a business rules engine. This engine would take in multiple BusinessRules as classes perhaps? Each one of these classes will have an analyze function on it.public interface BusinessRule {Boolean analyze(Object o); } This can be used ANYWHERE. It can be used to verify the state of your application. Verify data is correct. Verify that the user is logged in. Each one of these classes just needs to implement the analyze function, which will be different for each rule. Where as if we were creating a generic List object, the use of abstract classes would be better.

    • 3
  • Sreekanth Reddy
    Jun, 2018 26

    Abstract : If we feel that there is common logic result & different implementation as a container then choose abstract. Interface : If we feel the container contains only different implementions from clients then choose interface.

    • 3
  • Kapil Gaur
    Feb, 2019 12

    Watch this C# Corner video of mine. I think it covers all aspects of this question. https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-c-sharp-programming/

    • 2
  • Rahul Kadam
    Nov, 2018 5

    For declaring interface there should be requirement just like we say,different implementation in derived class. And for abstract class for same method implementation. Also help of interface we achieve SOLID principle.Interfaces also important for restriction of user.

    • 2
  • Mayank
    Sep, 2018 6

    Abstract class is used when we need to do some default implementation of functions.We can also do implementation of abstract method in derive class according to a requirement.But here we have two features that are missing in abstract class: 1. Derive class cant inherit multiple abstract class. 2 . Abstract class contain abstract methods and non abstract methods.Above two things can be resolved in interface .Derive class can inherit multiple interfaces.Interface contains only abstract methods.

    • 2
  • Pravin Lalge
    Aug, 2018 8

    Hello Arjun, Please see this linkhttps://www.c-sharpcorner.com/blogs/difference-between-abstract-class-interfaces

    • 2
  • Pravin Lalge
    Jul, 2018 13

    If you are dealing with small projects then go with Interface.If you are dealing with big projects then go with Abstract class.

    • 2
  • Kashif Asif
    Jul, 2018 6

    We can use multiple purpose like 1- To implement multiple inheritance in C# 2- Use abstract class when you want to provide pre-defined implementation as you can create methods without implementation in abstract class like Interface 3- When you want developer follow your own pre-defined pattern/method/properties etc. (In this case you can implement interface and abstract both)

    • 2
  • Sandhya Pothineni
    Dec, 2018 20

    whenever frequent changes are required better to go with Abstract.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS