Interface:
As we all know c# does not support multiple inheritance hence if you want to implement multiple inheritance in c# we need to use Interfaces.
In interface we just declare the method signature and the implementation we write after inheriting it inside the class.
Below is the example how the interface is declared. The methods signatures defined inside the interface is always declared without the access modifiers(i.e. public,private etc).
Interface Name should always start with I and then name i.e IAdd as displayed in the below example.
After the method signature is declared in the Interface. Now lets see how to define the method inside the class.
Abstract class
Abstract class can only be used as base class. we cannot create the object of this class. Thus all the common code we write it in the abstract class and use it in the derived class.Below example shows hows the abstract class works
An Abstract class can have both methods with implementations as well methods with out implementations
and also Abstract class can inherit from interfaces. But Abstract class do not support multiple inheritance.
As we all know c# does not support multiple inheritance hence if you want to implement multiple inheritance in c# we need to use Interfaces.
In interface we just declare the method signature and the implementation we write after inheriting it inside the class.
Below is the example how the interface is declared. The methods signatures defined inside the interface is always declared without the access modifiers(i.e. public,private etc).
Interface Name should always start with I and then name i.e IAdd as displayed in the below example.
After the method signature is declared in the Interface. Now lets see how to define the method inside the class.
The Interface IAdd is inherited inside the class and the method code is written here. If suppose we have declared 2 method signature inside the interface then we will have to write the code for both the two methods in the class.
When to use Interface ?
Interface should be used when your software design or plan is rigid and not changeable. suppose we have declared 2 method signatures in the interface and in future we need to add one more method in it then we will have to check all the class using that interface and will have to write the method implementation in all the class, for that newly declared method name in the interface. Whereas in abstract class there is no such problem , if the software design plan is flexible then go for abstract class.
Abstract class
Abstract class can only be used as base class. we cannot create the object of this class. Thus all the common code we write it in the abstract class and use it in the derived class.Below example shows hows the abstract class works
An Abstract class can have both methods with implementations as well methods with out implementations
and also Abstract class can inherit from interfaces. But Abstract class do not support multiple inheritance.
No comments:
Post a Comment