I'm new to C# and experimenting with interfaces, coming from the Swift world. I came across default implementations and they look like they could be super powerful.
However I hit a roadblock when I try to implement a method declared in the parent interface concretely (provide a default implementation). And if I ignore the warning and try to define a concrete class(see below), intellisense throws me a error asking me to implement Checkout()
method
CSO108: IPhysicalProduct.Checkout(' hides inherited member "Product Checkout(). Use the new keyword if hiding was intended.
Code that replicates the issue.
interface IProduct {
string name { get; }
void Checkout();
}
interface IPhysicalProduct : IProduct
{
void ShipItem() { Debug.Log("Shipping item"); }
public void Checkout() { ShipItem(); } //CSO108 here
}
interface IDigitalProduct: IProduct
{
int totalDownloadsLeft { get; }
void DownloadItem() { Debug.Log($"Downloading item; {totalDownloadsLeft} downloads left"); }
public void Checkout() { DownloadItem(); } //CSO108 here
}
class PhysicalProd : IPhysicalProduct
{
public string name => throw new NotImplementedException();
public void Checkout()
{
throw new NotImplementedException();
}
}
Is it at all possible for the inherited interfaces to define default implementations of the parent's declared methods?