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
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…