-4

i know in c++, inheritance is either "public" or "private" or "protected" that meas if i inherit class A publicly to class B as follows

    class A
{
    public int pub1;
    private int prvt1;
    protected int proc1;
}


class B : public A
{

    //public int pub1;//This variable is because of inheritacne and is internal.
    //public int proc1;//This variable is because of inheritacne and is internal.
    public int pub2;
    private int prvt2;
    protected int pro2;
}

i.e. two variables of class A, (pub1, proc1) got inherited but there access specifier is public. But in C# it is as follows

    class A
{
    public int pub1;
    private int prvt1;
    protected int proc1;
}


class B : A
{

    //public int pub1; //This variable is because of inheritacne and is internal.
    //protected int proc1;//This variable is because of inheritacne and is internal.
    public int pub2;
    private int prvt2;
    protected int pro2;
}

i.e. two variables of class A, (pub1, proc1) got inherited but there access specifier is same as what was there in class A.

why is this kind of implementation given in .NET framework. what are the pros and cons in this?

ismail baig
  • 861
  • 2
  • 11
  • 39
  • 1
    Actually the `prvt1` field is also inherited, but it can only be "seen" from inside `A`. For example if class `A` contains code that handles a `B` instance, it can change the field `prvt1` on `B`. – Jeppe Stig Nielsen Mar 26 '13 at 06:21
  • In a non-sealed, non-static `class`, there are two more accessibility levels possible: `internal int intnl1;` and `protected internal int protintnl1;` – Jeppe Stig Nielsen Mar 26 '13 at 06:24

2 Answers2

1

Maybe this is a comment, but its too big to fit in the comment area.

I took your class definitions and tried this out -

class A
{
    public:
    int pub1;
    private:
    int prvt1;
    protected:
    int proc1;
};


class B : public A
{

    //public int pub1;//This variable is because of inheritacne and is internal.
    //public int proc1;//This variable is because of inheritacne and is internal.
    public:
    int pub2;
    private:
    int prvt2;
    protected:
    int pro2;
};

int main()
{
    B* b = new B();
    b->pub2 = 1;
    b->proc1 = 2;
}

The compiler barked at me -

prog.cpp: In function ‘int main()’:
prog.cpp:8:9: error: ‘int A::proc1’ is protected
prog.cpp:29:8: error: within this context

I guess C++ doesn't convert protected member into public. Or am I missing something from the question?

Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
  • C++ does convert protected to public, but if the inheritacne way is "protected". But if we inherit publicly (as was done in teh above) example, then it will not. – ismail baig Mar 26 '13 at 07:43
  • Privately Inherited: Then the access specifiers of the inherited class also are private , 2. Protectedly Inherited: Then the access specifiers of the inherited class also are protected , 3. Publicly Inherited: Then the access specifiers of the inherited variable is unchanged. – ismail baig Mar 26 '13 at 07:44
  • AFAIK I don't think C# allows you to access control inheritance. The access modifiers of the base is what the derived types see (or not). I was curious about publicly inherited protected members of C++, and looks like Protected members of A remain so, even if base class is publicly inherited? – Srikanth Venugopalan Mar 26 '13 at 08:47
1

This answer is only about C#.

In C#, there's only one "level" of inheritance, and that is a kind of "public" inheritance. Your class A can have members of five different accessibilities, namely:

class A
{
    public int pub1;
    private int prvt1;    // "private" keyword can be left out
    protected int proc1;
    internal int intnl1;
    protected internal int protintnl1;
}

When a public or protected member is inherited, it still has exactly the same accessibility in the derived class. When a private member is inherited, it is not accessible from the deriving class, so in a sense it is super-private. Similarly, when an internal member is inherited by a type in another assembly the internal member becomes invisible to the derived class.

And so, when a protected internal member is derived by a class in another assembly it becomes effectively protected. This can be seen when the member is overridden. Consider the example:

public class Ax
{
    protected internal virtual void Method()
    {
    }
}

And then inside another assembly:

// Bx is in another assembly than Ax
class Bx : Ax
{
    protected override void Method()
    {
    }
}

Note that Method has effectively become protected inside the new assembly.

Inside a struct, it is not allowed to declare new protected (or protected internal) members, since in C# structs are sealed, and so it is meaningles to introduce protected members. (There's one protected method, MemberwiseClone(), that structs inherit from their base class.)

Similarly a static class cannot declare new protected (or protected internal) members. A sealed class may inherit many protected members, and override some of them, but it gives a compiler warning to introduce a new protected member in a sealed class.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181