'Why are interfaces turning into abstract classes c#
I'm honestly looking for good examples here of reasons to use interfaces over abstract. And the main reason I can see is they are unopinionated public blueprints for classes to follow. But with them adding default implementations thats no longer true. Its becoming more and more difficult to distinguish interfaces from abstract classes. Genuinely whats the point in using an interface over an abstract class in c# 11. Its just a slower abstract class.
Solution 1:[1]
Even I find abstract classes and interfaces are too similar. I prefer using abstract classes, as its methods can be defined, until and unless there's the concept of multiple inheritance used.
As with case of abstract classes, we can't inherit from multiple abstract classes, whereas with interfaces, you could inherit as many interfaces you wish.
I'll let you understand this through a patch of code.
interface IFirstInterface
{
void myMethod();
}
interface ISecondInterface
{
void myOtherMethod();
}
class Democlass : IFirstInterface, ISecondInterface
{
public void myMethod()
{
System.Console.WriteLine("Some text..");
}
public void myOtherMethod()
{
System.Console.WriteLine("Some other text..");
}
}
class Program
{
static void Main(string[] args)
{
Democlass myObj = new Democlass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
Lemme know if you find this unclear.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeremy Caney |
