I am using an interface reference variable to access the properties on an Interface
But in addition to that the class that implements the interface has its own attributes.
I am unable to access the class attributes through this interface reference.
Here are my questions:
1) why is that so?
2) What is the solution to the problem? Is there any way i can access the coolant power variable in AC class through machine only? Will a TYPE CAST work?
interface IMachines
{
#region properties
int machineID { get; set; }
static int totalID { get; set; }
string name { get; set; }
string make { get; set; }
int weight { get; set; }
int cost { get; set; }
int warranty { get; set; }
DateTime creationDate { get; set; }
#endregion
int generateWarrantyExpiry();
int searchMachine();
}
public class AC:IMachines
{
#region ACMembers
protected int _machineID;
protected string _name;
protected int _weight;
protected string _make;
protected DateTime _creationDate;
protected int _warranty;
protected int _cost;
public int _coolentPower;
public int CoolentPower
{
get { return _coolentPower; }
set { _coolentPower = value; }
}
#endregion
#region IMachines Members
public int machineID
{
get { return _machineID; }
set { _machineID = value; }
}
public string name
{
get { return _name; }
set { _name = value; }
}
public string make
{
get { return _make; }
set { _make = value; }
}
public int weight
{
get { return _weight; }
set { _weight = value; }
}
public int cost
{
get { return _cost; }
set { _cost = value; }
}
public int warranty
{
get { return _warranty; }
set { _warranty = value; }
}
public DateTime creationDate
{
get { return _creationDate; }
set { _creationDate = value; }
}
public int searchMachine()
{
//Search machine logic to be implemented
return 2
}
public void GenerateWarranty()
{
//generate warranty logic to be implemented
}
#endregion
}
}
Note that using a cast, as many answerers have suggested, will break the abstraction offered by the
IMachinesinterface.If it’s true that you’ll only ever be using the one type that implements the interface,
AC, then this will work, but if you ever want to support some other type ofIMachines, things may break down.