class Customer
{
protected int _cid, _bal, _status;
private string _cname;
public Customer(int _cid, int _bal, int _status, string _cname)
{
this._cid = _cid;
this._bal = _bal;
this._cname = _cname;
this._status = _status;
}
public int Cid
{
get
{
return _cid;
}
}
public virtual string Cname
{
get
{
return _cname;
}
set
{
if (_status != 0 & _bal >= 500)
{
_cname = value;
}
}
}
public int Bal
{
get
{
return _bal;
}
set
{
if (_status != 0 & value >= 500)
{
_bal = value;
}
}
}
public int Status
{
get
{
return _status;
}
set
{
_status = value;
}
}
public virtual void display()
{
Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", _cid, _cname, _bal, _status);
}
}
class Specialcustomer : Customer
{
public Specialcustomer(int _cid, int _bal, int _status, string _cname)
: base(_cid, _bal, _status, _cname)
{
}
public override string Cname
{
get
{
return _cname;
}
set
{
if (value == "Special")
{
_cname = value;
}
}
}
public override void display()
{
Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", Cid, Cname, Bal, Status);
}
}
static void Main(string[] args)
{
Customer C1 = new Specialcustomer(10, 400, 1, "BOND");
C1.display();
C1.Cname = "Special";
C1.display();
Console.ReadKey();
}
What I have tried:
in this example
when i was using
_cname
as protected field it was working fine but when i changed it to private it is giving me error.so how can i access the
_cname
in child class if i changed the field to pvt. without using
Reflection.
and if i changed _cname to base.Cname in Special Customer class Overriding is not happening as it going to base property