Click here to Skip to main content
13,293,474 members (66,469 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 1 Jun 2009

Explicit Implementation of Interface

, 10 Jun 2009
Rate this:
Please Sign up or sign in to vote.
Polymorphism using interfaces


Achieving polymorphic behavior by implementing interfaces is a well known pattern. I am also putting some thoughts and code to demonstrate the same. This article clearly demonstrates the use of interfaces and their importance when they are implement explicitly.


Few of my colleagues and developers have been questioning me when I suggest that they use interfaces to create the programming model. Why should we write extra lines of code in creating an interface, then implementing it in a class while we can directly use the class without writing the interface.

So, if anyone has the same question, probably this post can answer it.

Using the Code  

Simply download the code, compile and execute it.

In this sample application, I picked cricket as a domain and created few of the interfaces representing behaviors of the players.

Below are the interfaces:

 /// <summary>
 /// Interface exposing batsman methods and properties
 /// </summary>
 public interface IBatsman
{  //Properties
  int BatingOrder { get; set; }
  double StrikRate { get; set; }
  int HighestScore { get; set; }
  int Centuries { get; set; }
  void StartBating();
/// <summary>
 /// /// Interface exposing bowler methods and properties
 /// </summary>
 public interface IBowler {  //Properties
  int WicketCount { get; set; }
  double AvgBallingSpeed { get; set; }
  int BowlingCategory { get; set; }
  void StartBowling();
 /// <summary>
 /// Interface exposing allrounder methods and properties 
 /// (an allrounder has batsman and bowler capabilities)
 /// </summary>
public interface IAllRounder : IBatsman , IBowler
{  int StumpingCount { get; set; }
  int CatchCount { get; set; }
  void ChooseRole();
 /// <summary>
 /// Interface exposing cricketer methods and properties 
 /// (a cricketer can be an allrounder or a batsman or a bowler)
 /// </summary>
 public interface ICricketer : IAllRounder , IBowler , IBatsman {
  int ICCRank { get; set; }
  void PlayCricket();

Here is the Cricket class which implements all of these interfaces. Since every cricketer has some other properties also which are not covered in these interfaces, I am defining such properties in the class itself, i.e. name (defining only one for example). 

/// <summary>
/// Cricketer class implements all the interfaces and provides a provision 
/// to instantiate the right cricketer 
/// </summary>

<pre> public class Cricketer : ICricketer , IAllRounder , IBowler , IBatsman
  #region Constructor
    	public Cricketer() { }
#region Properties public string Name { get; set; } #endregion #region Method public void ParticipateInTournament() { } #endregion #region ICricketer Members int ICricketer.ICCRank { get; set; } void ICricketer.PlayCricket(){} #endregion #region IAllRounder Members int IAllRounder.StumpingCount { get; set; } int IAllRounder.CatchCount { get; set; } void IAllRounder.ChooseRole(){} #endregion #region IBatsman Members int IBatsman.BatingOrder { get; set; } double IBatsman.StrikRate { get; set; } int IBatsman.HighestScore { get; set; } int IBatsman.Centuries { get; set; } void IBatsman.StartBating(){} #endregion #region IBowler Members int IBowler.WicketCount { get; set; } double IBowler.AvgBallingSpeed { get; set; } int IBowler.BowlingCategory { get; set; } void IBowler.StartBowling() {} #endregion }

So, the model is ready to be used and the client application has to instantiate the class referring to the correct interface.

The beauty is that the object of the same class will have different properties/methods based on the interface used.

Here is the example code snippet:

class Program
  static void Main(string[] args)
   /*If IBowler interface is used, then object exposes only IBowler members*/
   IBowler Murlidharan = new Cricketer();
   Murlidharan.WicketCount =300;
   /*If IBatsman interface is used, then object exposes only IBatsman members*/
   IBatsman SachinTendulkar = new Cricketer();
   SachinTendulkar.BatingOrder = 10;

   /*If IAllRounder interface is used, then object exposes only IAllRounder members
   which in turn exposes members of IBowler and IBatsman interfaces too*/
   IAllRounder SorabGanguly = new Cricketer();
   //IAllRounder member
   //IBowler member
   SorabGanguly.WicketCount = 15;
   //IBatsman member
   SorabGanguly.BatingOrder = 10;
   /*If ICricketer interface is used, then object exposes only ICricketer members
    which in turn exposes members of IAllRounder, 
    IBowler and IBatsman interfaces too*/
   ICricketer Kaipldev = new Cricketer();
   //ICricketer members
   Kaipldev.ICCRank = 10;
   //IAllRounder member
   //IBowler member
   Kaipldev.WicketCount = 15;
   //IBatsman member
   Kaipldev.BatingOrder = 10;

   /*Exposes the members of only Cricketer class*/

   Cricketer AnyCricketer = new Cricketer();
   AnyCricketer.Name = "Vinod";

Points of Interest

It was real fun writing this code snippet. Hope it will be useful for others too.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vinod Kumar Gupta
Software Developer (Senior) Microsoft
India India
I am an MCA from Barkatullah University Bhopal.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Richard Deeming12-Jun-09 9:45
memberRichard Deeming12-Jun-09 9:45 
GeneralRe: My vote of 2 Pin
Vikas_Awasthi14-Jun-09 20:28
memberVikas_Awasthi14-Jun-09 20:28 
QuestionHow to implement interfaces Pin
sonnendeck11-Jun-09 21:36
membersonnendeck11-Jun-09 21:36 
GeneralMy vote of 2 Pin
Avathar11-Jun-09 13:55
memberAvathar11-Jun-09 13:55 
GeneralRe: My vote of 2 Pin
Vikas_Awasthi11-Jun-09 23:52
memberVikas_Awasthi11-Jun-09 23:52 
GeneralGood one ! Pin
Vikas_Awasthi10-Jun-09 5:48
memberVikas_Awasthi10-Jun-09 5:48 
Best thing about the article is its example driven explanation, which is making it very easy to understand interfaces. Smile | :)

But,I think the word "Explicit" is a bit confusing here..

Because C# provides 2 types of implementation 1)explicit and 2) implicit. If we go with these terminologies the subject line would suggest a different meaning.

Explicit implementation comes into picture when there are 2 interfaces implemented into one class and both interfaces are having a method with the same name n same signature.
Generalnice way to illustrate Interface! Pin
chandan kislay9-Jun-09 20:59
memberchandan kislay9-Jun-09 20:59 
GeneralRe: nice way to illustrate Interface! Pin
Vinod Kumar Gupta10-Jun-09 5:07
memberVinod Kumar Gupta10-Jun-09 5:07 
GeneralThanks Pin
GavinSV1-Jun-09 13:40
memberGavinSV1-Jun-09 13:40 
GeneralRe: Thanks Pin
Vinod Kumar Gupta10-Jun-09 4:36
memberVinod Kumar Gupta10-Jun-09 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Jun 2009
Article Copyright 2009 by Vinod Kumar Gupta
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid