Click here to Skip to main content
15,885,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to access inherited fields in my constructor and method but I cant get them because they are private. => by making the fields public I can easily access them but at the same time the fields are no longer secure.

So, my question is : How can I access inherited private fields?

Any comments or suggestions are welcome. Thank you.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    interface IAnimal
    {
        void sleep();
        void eat();
        void breathe(bool i);
    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    abstract class Animal : IAnimal
    {
        byte legs;
        bool tail;
        string name;
        bool breathing;
        byte breathingRate;

        public void sleep()
        {

        }

        public void eat()
        {

        }

        public void breathe(bool i)
        {
            breathing = i;
        }

        public abstract void setBreathingRate();
    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    sealed class Cat : Animal
    {
        public Cat(byte _legs, bool _tail, string _name)
        {
            legs = _legs;
            tail = _tail;
            name = _name;
        }

        public override void setBreathingRate()
        {
            // breathing rate will be different for each animal
            if (breathing)
            {
                breathingRate = 110;
            }
            else
            {
                breathingRate = 0;
            }
        }
    }
}
Posted
Comments
PIEBALDconsult 17-Oct-13 10:45am    
I suggest always specifying the access level (in this case use protected) and never accepting the default. Don't expect the reader to know what the defaults are in every situation.

Also, you should probably mark the methods in the abstract class as either abstract or virtual.
dedo1991 17-Oct-13 12:28pm    
thanks for your suggestion I appreciate it

use the protected keyword if you have access to the code you want to derive from. In General, a look at http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^] is maybe a good idea.

You don't have the code you want to derive (because it's in closed source lib) - you are out of look. The only Thing you could do in this case is using "reflection" (which can be error-prone, and has bad performance), feel free to ask if you have any further questions to this ropic..
 
Share this answer
 
v2
Comments
PIEBALDconsult 17-Oct-13 10:41am    
"can be error-prone" -- certainly not future-proof, but "error-prone" may be overstating the problem.
"has bad performance" -- only the first time; cache the info and reuse it.
johannesnestler 18-Oct-13 4:07am    
"not future-proof" is a better way to describe the problems in the long run, if you have to "open" 3rd party code this way - thank you! Sorry my answer wasn't very well formulated and targeted a "beginner" OOP question (as I asumed from OP origingal code). But I think SA added the usefull info about reflection in between. And you are completly right about caching the info and get acceptable performance...
You should not access private members in the derived class. This is what private access was designed for: to prevent access from anywhere except the declaring class itself. To make a member accessible from a derived class, you should give it any access specifier except private: it could be either protected, protected internal, internal or public. Note that it's important to give only the minimal required access, no more.

If you really want to abuse these access rules (in some really rare cases this is really useful), reflection is at your service. That said, access specifiers are only to protect a developer from himself, not a security device. I'm not sure if reflection is relevant to your question (most likely, it is not), so you can learn about it by yourself or ask your follow-up questions if you have to:
http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29[^],
http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.aspx[^].

—SA
 
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