KEY POINTS
PROGRAM.CS
namespace Contravariance
{
public interface IPlay< in T>
{
void PlayCricket(T x);
}
public class Play : IPlay
{
public void PlayCricket(T x)
{
if(x is ODICricket)
(x as ODICricket).Play();
if (x is T20Cricket)
(x as T20Cricket).Play();
}
}
public class Cricket
{
public virtual void Play()
{
Console.WriteLine("cricket play");
}
}
public class ODICricket : Cricket
{
public override void Play()
{
Console.WriteLine("ODICricket play");
}
}
public class T20Cricket : Cricket
{
public override void Play()
{
Console.WriteLine("T20Cricket play");
}
}
}
PROGRAM.CS
using Contravariance;
IPlay cricketer1= new Play();
cricketer1.PlayCricket(new T20Cricket());
IPlay cricketer2 = new Play();
cricketer2.PlayCricket(new ODICricket());
OUTPUT
T20Cricket play
ODICricket play
FINAL TAKE
- public interface IPlay< in T>
- Interface<childClass> obj= new ClassName<ParentClass>();
- className implements interface