Consider this example:
public interface IAccount
{
string GetAccountName(string id);
}
public class BasicAccount : IAccount
{
public string GetAccountName(string id)
{
throw new NotImplementedException();
}
}
public class PremiumAccount : IAccount
{
public string GetAccountName(string id)
{
throw new NotImplementedException();
}
public string GetAccountName(string id, string name)
{
throw new NotImplementedException();
}
}
protected void Page_Load(object sender, EventArgs e)
{
IAccount a = new PremiumAccount();
a.GetAccountName("X1234", "John"); //Error
}
How can I call the overridden method from the client without having to define a new method signature on the abstract/interface (since it is only an special case for the premium account)? I’m using abstract factory pattern in this design… thanks…
Well, considering it’s only defined for the
PremiumAccounttype, the only way you know you can call it is ifais actually aPremiumAccount, right? So cast to aPremiumAccountfirst: