Using Interface in C# Language
Objective
In this tutorial we will show you about Interaces in C#. This is also important definition, beside abstract and inheritance definition. Interface make your program conform to a clearly structure and defined architecture.
Let's go
What happens if you want some classes conform to a contract, here we mean if you want all the derived class must implement the same members/methods from its parents. You can write a base/parent class and let derived class implements that base class. But if you have more than one base class, so you cannot do that. C# doesn't allow you to implement more than one base class. Alternatively, you can use Inteface. Let see the code snippet bellow to understand of how to declare an interface:
- public interface IMachine
- {
- //method of the interface
- void DoOperation();
- //method of the interface
- void increaseSpeed(int v);
- //method of the interface
- void decreaseSpeed(int v);
- }
In above example we declare an Interface named IMachine, with three members (methods) includes DoOperation, increaseSpeed and decreaseSpeed. When a class inherits this interface, it must implement these methods. A class can inherit multiple interface. If a base class inherits from an interface, any classes that derive from that base class inherit that implementation. In C# language you need to note that only a class can inherit an interface, an Interface cannot inherit from an other interface. An interface however can inherit from a base interface. In this case, a derived interfacecannot override the members/methods from the base Interface, it only declare a new members. In case of a class inherits from multiple interface, the class must implements all members/methods from these interface. A note is that if these interfaces have the same methods/members name, the derived class will must declare the methods/members with the same name. That is true if the members/methods (with the same name) have the same input parameters. Let see an example bellow:
- public interface IMachine
- {
- //method of the interface
- void DoOperation();
- //method of the interface
- void increaseSpeed(int v);
- //method of the interface
- void decreaseSpeed(int v);
- }
- public interface ITool
- {
- //method of the interface
- void DoOperation();
- }
- public class Car : IMachine, IEquipment
- {
- public void DoOperation()
- {
- }
- public void increaseSpeed(int v)
- {
- }
- public void decreaseSpeed(int v)
- {
- }
- }
- public class Car : IMachine, ITool
- {
- //explicit implementation with interface IEquipment
- public void IEquipment.DoOperation()
- {
- }
- public void increaseSpeed(int v)
- {nc
- }
- public void decreaseSpeed(int v)
- {
- }
- }
Summary
In this tutorial you have been shown some very helpful information about interface, includes:
- Definition and how to declare an interface
- Inherit from multiple interfaces
With interface you can do many things to reuse and make flexibility to you program. Just try to use it in a right way.
Add new comment
- 169 views