Today we will discuss the differences between an Interface and Abstract Class. For the beginners, it’s a little bit difficult to understand the concept and difference between them. As we know in C# multiple inheritance is not allowed but we can achieve this by using Interface and this is the main difference between them, A class can implement more than one Interface but can inherit only one abstract class.
However, let’s have a look on some other features and differences.
1. Default Implementation: An interface does not have any type of code implementation it just provides the signature of methods and properties but An Abstract class can have code implementation, definition and may have details to be overridden.
2. Access Modifier: As we know interface provide signature and which is going to implement, this interface, that will define and provide the code implementation so in Interface there is no access modifier all properties and functions are by default public whereas an abstract class can have access modifier for subs, properties and functions.
3. Multiple Inheritance: Interface provide multiple inheritance in C# by inheriting multiple interfaces whereas a class may inherit only one abstract class.
4. Performance: Interfaces are slower than abstract class because an interface requires more time to check the signature and exact method in related classes.
5. Changes or Add New Functions: when we update or add new function to an interface then we have to trace all implementations of interface and need to update all references which is a tough task. In abstract class if we add some new function then we have default code implementation, due to this, all code will work properly no need to change.
Now let's see code implementation.
Abstract Class: abstract class cannot be instantiated so its methods can be called with class name or by using inherited class object.
abstract class ABS_CLS1
{
public int Addition(int a, int b)
{
return (a + b);
}
}
class Simple_CLS : ABS_CLS1
{
public int Multiplication(int a, int b)
{
return a * b;
}
}
class Check_Impl
{
static void Main(string[] args)
{
Simple_CLS ob = new Simple_CLS();
int Total = ob.Addition(5, 10);
Console.WriteLine("Result is {0}", Total);
Console.ReadLine();
}
}
In the above example, we can see, we created the object of class Simple_CLS which inherited our abstract class, so, by using that object we call abstract class method so it clears one more thing abstract class provide the public method in it and also those members which inherited from abstract class's base class.
Interface: interface is not a class, it is by self an entity nominated with word Interface.
interface TestInterface
{
void testMethod();
}
class CallClass : TestInterface
{
public static void Main()
{
CallClass cls = new CallClass();
cls.testMethod();
}
public void testMethod()
{
Console.WriteLine("Called Interface method from implemented class");
Console.ReadLine();
}
}
As interface, cannot have code implementation so only one method signature is in interface and calling class have code implementation of that method because its mandatory to calling class implement interface method and properties.
Comments and Suggestions are always welcome!
No comments:
Post a Comment