Click here to Skip to main content
Click here to Skip to main content

Explicit Implementation of Interface

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

Introduction 

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.

Background

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; }
  //Method
  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; }
  //Method
  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; }
  //Method
  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; }
  //Method
  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() { }
  #endregion  
#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;
   Murlidharan.StartBowling();
 
   /*If IBatsman interface is used, then object exposes only IBatsman members*/
   IBatsman SachinTendulkar = new Cricketer();
   SachinTendulkar.BatingOrder = 10;
   SachinTendulkar.StartBating(); 

   /*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
   SorabGanguly.ChooseRole();
   //IBowler member
   SorabGanguly.WicketCount = 15;
   SorabGanguly.StartBowling();
   //IBatsman member
   SorabGanguly.BatingOrder = 10;
   SorabGanguly.StartBating();   
 
   /*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;
   Kaipldev.PlayCricket(); 
   //IAllRounder member
   Kaipldev.ChooseRole();
   //IBowler member
   Kaipldev.WicketCount = 15;
   Kaipldev.StartBowling();
   //IBatsman member
   Kaipldev.BatingOrder = 10;
   Kaipldev.StartBating(); 

   /*Exposes the members of only Cricketer class*/

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

Points of Interest

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

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 2 PinmemberRichard Deeming12-Jun-09 9:45 
This is classified as an "Advanced" article, yet it is clearly aimed at beginners.
GeneralRe: My vote of 2 PinmemberVikas_Awasthi14-Jun-09 20:28 
QuestionHow to implement interfaces Pinmembersonnendeck11-Jun-09 21:36 
GeneralMy vote of 2 PinmemberAvathar11-Jun-09 13:55 
GeneralRe: My vote of 2 PinmemberVikas_Awasthi11-Jun-09 23:52 
GeneralGood one ! PinmemberVikas_Awasthi10-Jun-09 5:48 
Generalnice way to illustrate Interface! Pinmemberchandan kislay9-Jun-09 20:59 
GeneralRe: nice way to illustrate Interface! PinmemberVinod Kumar Gupta10-Jun-09 5:07 
GeneralThanks PinmemberGavinSV1-Jun-09 13:40 
GeneralRe: Thanks PinmemberVinod Kumar Gupta10-Jun-09 4:36 

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

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

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