Interface Implementation

Why Trust Techopedia

What Does Interface Implementation Mean?

Interface implementation, in C#, refers to the inheritance of an interface by a struct or class that provides the functionality for the members declared in the interface. The members of the implemented interface can include methods, properties, indexers and events.

Advertisements

In general, an interface represents a contract which has to be adhered by a class or struct that implements all the members specified in the interface. The key benefits of interface implementation include flexibility, low coupling, high cohesion and polymorphic behavior.

In C#, interface implementation allows a class to inherit from multiple interfaces instead of multiple classes so that more than one behavior can be inherited through multiple interfaces that share method signatures only. Interface can be implemented as property to specify whether the property is read-write, read-only or write-only. An interface can be implemented explicitly to hide a member and replace it with another. Unlike in Java, C#‘s explicit interface implementation allows multiple interfaces with same method name and signatures, to have different implementations without any name clash and provide different result based on the current cast of the object.

Techopedia Explains Interface Implementation

Similar to an abstract class, an interface does not provide a default implementation. A member of a class or struct that implements a member of an interface must be public, non-static and possess the same name and signature as that in interface.

For example, an interface IAccount can be implemented by a class, SavingAccount which implements the members of IAccount that can include BalanceAmount, DateOfOpening, etc. as its properties and methods like PrintStatement, CalculateInterest, etc.

Interface implementation can be implicit when a base class of a derived class already implements the interface. All the base interfaces need not be explicitly specified in the base class list of the derived class. In addition, the derived class can change the interface behavior that has been defined already in the base class by overriding the virtual members in the derived class.

Interface implementation is explicit when members of two or more interfaces having same name with same signature but performing two different functions, are inherited by a class or struct that implements each member explicitly. Such a member is named with the name of the interface and a period and can only be accessed only through an instance of the interface. Unlike implicit methods, explicit methods cannot be abstract or virtual.

Advertisements

Related Terms

Margaret Rouse
Technology Expert
Margaret Rouse
Technology Expert

Margaret é uma premiada redatora e professora conhecida por sua habilidade de explicar assuntos técnicos complexos para um público empresarial não técnico. Nos últimos vinte anos, suas definições de TI foram publicadas pela Que em uma enciclopédia de termos tecnológicos e citadas em artigos do New York Times, Time Magazine, USA Today, ZDNet, PC Magazine e Discovery Magazine. Ela ingressou na Techopedia em 2011. A ideia de Margaret de um dia divertido é ajudar os profissionais de TI e de negócios a aprenderem a falar os idiomas altamente especializados uns dos outros.