Skip to content

Instantly share code, notes, and snippets.

@fschwiet
Last active September 13, 2022 20:24
Show Gist options
  • Save fschwiet/f337b850e65b2c6361a4c41724723ca3 to your computer and use it in GitHub Desktop.
Save fschwiet/f337b850e65b2c6361a4c41724723ca3 to your computer and use it in GitHub Desktop.
experimenting with default interface implementation overrides in C#
using System;
public interface ISparrow
{
double GetAirspeed() => 1.0;
}
public class AfricanSparrow : ISparrow
{
public double GetAirspeed() => 2.0;
}
public class EuropeanSparrow : ISparrow
{
public double GetAirspeed() => 3.0;
}
public class WestEuropeanSparrow : EuropeanSparrow
{
public double GetAirspeed() => 4.0;
}
public class EastEuropeanSparrow : EuropeanSparrow
{
public double GetAirspeed() => 5.0;
}
public interface IArcticPoleSparrow : ISparrow
{
double GetAirspeed() => 6.0;
}
public class NorthPoleSparrow : IArcticPoleSparrow
{
public double GetAirspeed() => 7.0;
}
public class SouthPoleSparrow : IArcticPoleSparrow
{
public double GetAirspeed() => 8.0;
}
public class Program
{
public static void Main()
{
var african = new AfricanSparrow();
var western = new WestEuropeanSparrow();
var eastern = new EastEuropeanSparrow();
var northern = new NorthPoleSparrow();
var southern = new SouthPoleSparrow();
Console.WriteLine(african.GetAirspeed());
Console.WriteLine(((ISparrow)african).GetAirspeed());
Console.WriteLine(western.GetAirspeed());
Console.WriteLine(((ISparrow)western).GetAirspeed());
Console.WriteLine(eastern.GetAirspeed());
Console.WriteLine(((ISparrow)eastern).GetAirspeed());
Console.WriteLine(northern.GetAirspeed());
Console.WriteLine(((IArcticPoleSparrow)northern).GetAirspeed());
Console.WriteLine(((ISparrow)northern).GetAirspeed());
Console.WriteLine(southern.GetAirspeed());
Console.WriteLine(((IArcticPoleSparrow)southern).GetAirspeed());
Console.WriteLine(((ISparrow)southern).GetAirspeed());
}
}
/* output:
2
2
4
3
5
3
7
7
7
8
8
8
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment