Skip to content

Instantly share code, notes, and snippets.

@iAvinashVarma
Last active March 10, 2017 05:27
Show Gist options
  • Select an option

  • Save iAvinashVarma/ac0a17aceec18e653cff2d10610d6a81 to your computer and use it in GitHub Desktop.

Select an option

Save iAvinashVarma/ac0a17aceec18e653cff2d10610d6a81 to your computer and use it in GitHub Desktop.
Simple example to understand the inheritance, polymorphism and interface.
using System;
namespace NeedOfPolymorphism
{
enum UOM
{
Gram,
Litre
}
interface IRedLabel
{
void GetRedLabel();
}
interface IShopping
{
void BringItem(double weight, UOM uom);
}
class RedLabel : IRedLabel, IShopping
{
public void BringItem(double value, UOM uom)
{
Console.WriteLine("Son : Going to bring {0} {1}s.", value, uom.ToString().ToLower());
}
public virtual void GetRedLabel()
{
Console.WriteLine("Son : For whom I need to bring Red Label.");
}
}
class MotherRedLabel : RedLabel
{
public override void GetRedLabel()
{
Console.WriteLine("Son : 500 Grams or 1 KG.");
}
}
class FatherRedLabel : RedLabel
{
public override void GetRedLabel()
{
Console.WriteLine("Son : 750 ML or 1 Litre.");
}
}
class Program
{
static void Main(string[] args)
{
RedLabel redLabel = new RedLabel();
// ఎవరు అడిగారో తెలియక పోతే
redLabel.GetRedLabel();
// అమ్మ అడిగితే
redLabel = new MotherRedLabel();
redLabel.GetRedLabel();
redLabel.BringItem(500, UOM.Gram);
// నాన్న అడిగితే
redLabel = new FatherRedLabel();
redLabel.GetRedLabel();
redLabel.BringItem(0.75, UOM.Litre);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment