Interface in OOPS

Vikas Ubale
3 min readJun 5, 2019

--

Definition:

An interface is a contract between itself and any class that implements it. Interface can have methods, properties, or events. It contains only declaration of its members and implementation of its members will be given by the class who implements the interface. Interface makes it easy to maintain the program. Following are the specified terms of interface.

  • An interface can be defined by using interface keyword.
  • Interface can’t have private members.
  • By default all members of interface are public.
  • Interface enforces to the class for implementing its members in a class.
  • We can implementing the interface in two different ways as following,

1. Implicit

2. Explicit

Syntax of interface declaration:

Syntax of implementing interface:

Example:

  1. Implicit interface:
  • Here we declare interface and given name as InterfaceCompany and EmployeeDetails() is a method which is member of InterfaceCompnay.
  • Now implement the interface to a class Employee by given colon operator Like, Class Class_Name : Interface{ }
  • The implementation of interface methods in done by class Employee.
  • So after implementing the code in class now we can create the object of that interface and call the interface methods in main method.

2. Explicit Interface:

Another one type of interface is explicit interface. We can use multiple interfaces in our program so there is some problems occurred if same method name from different interface, so here we can use explicit interface.

  • In above example we created two interface InterfaceCompnay, and InterfaceDepartment.
  • At sometimes there have need of multiple interfaces and there some methods are same name.
  • In above class Employee there have multiple interfaces implemented.
  • There is EmployeeDetails() method name is same in both interface, so here lots of confusion in class for implementing the EmployeeDetails() method. So here need of implement explicit interface.
  • We use interface name for accessing the methods. Because of that we got exact methods for implementing.
  • Let’s see in the main class created different objects of the interface.And we can see the different output as per our requirement.

Conclusion:

So here we conclude that the interface only has members and that members are implemented by the class. There have two types of interface like implicit and explicit. We use interface keyword for declaring the interface.

If you are new to programming then one of the best way is to learn programming with project following we have one such video which demonstrate practical using C# and .NET: -

--

--