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 is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.