Click here to Skip to main content
15,891,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
        { //read only property
            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);
            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
Posted
Updated 11-Apr-17 21:25pm
v2
Comments
Tomas Takac 12-Apr-17 3:19am    
You can either make it protected or set the field via the base class property (base.Cname).
Akhil Jain 12-Apr-17 3:28am    
i can't make it base.Cname Overriding feature will not work as it will go to base Cname property.

1 solution

Quote:
as protected field it was working fine but when i changed it to private it is giving me error
This is the very purpose of the protected specifier. See, for instance inheritance - Are private members inherited in C#? - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900