Both the adapter class and the target class implement the same interface…why can I not treat them like the same object?
interface ISmartPhone
{
string Name { get; set; }
string Type { get; set; }
void ShowTextWithImage();
}
public class BasicFlipPhoneAdapter : ISmartPhone
{
IBasicPhone basicPhone;
public BasicFlipPhoneAdapter(IBasicPhone basicPhone)
{
this.basicPhone = basicPhone;
}
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
basicPhone.ShowBasicText();
}
}
public class iPhone : ISmartPhone
{
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
Console.WriteLine("O.o cool image!");
}
}
Error occurs:
iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
Because they’re not the same object type. A
BasicFlipPhoneAdapteris not aniPhone. What you should be able to do is:Note the type of the
flipPhoneAdaptervariable – it’sISmartPhone, notiPhone.