#451 – Implement Interface Explicitly to Simplify How a Class Appears to Clients

When you implement an interface explicitly, clients of the class can only see members of the interface if they access the object through the interface.

Assume that you have a Cow class which implements both the IMoo and the IStrangeCowBehavior interfaces.

public class Cow : IMoo, IStrangeCowBehavior

Also assume that the Cow class implements IStrangeCowBehavior explicitly, but implements IMoo normally.  Now a variable of type Cow will have access to the IMoo methods, but not the IStrangeCowBehavior methods.

If client code wants access to the IStrangeCowBehavior methods in Cow, it will need to access them via a variable of type IStrangeCowBehavior.

We’ve simplified things because most code that works with Cow objects won’t need access to the Burp, Dance, and DriveTractor methods and won’t even see the methods.  But code that needs these methods can get at them by using the appropriate interface variable.

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers