Childs are accessing private parts of parent class.

Hi,
Lets consider two classes. Class A who’s code is:

public class A {
    public void A_PublicMethod() { }
    private void A_PrivateMethod() { }
    protected void A_ProtectedMethod() { }
}

Then class B who’s code is:

public class B : A {
    public void B_PublicMethod() { }
    private void B_PrivateMethod() { }
    protected void B_ProtectedMethod() { }
}

Then I modify the class A like this:

public class A {
    public void A_PublicMethod()
    {
        B a_B_classInstance = new B();

       /*derived class should not access the private member of parent class and what the!! There is no method named "A_PrivateMethod()" in class B!!!*/
        a_B_classInstance.A_PrivateMethod();
    }
    private void A_PrivateMethod() { }
    protected void A_ProtectedMethod() { }
}

As you can see in the comment I wrote, why can the object of class B can access private parts of class A?

Childs should only access the public and protected parts of parent class, isn’t it?

I believe you can access private members in the parent class from within the parent class. So this is true for nested classes and child class instances within the parent.

The class B that you declare is within the scope of A, giving it access to private members. If you tried to call “A_PrivateMethod” from within B, you would not have access.

1 Like

But isn’t it logically incorrect?

It is definitely not intuitive, but it is logically correct.

Class B is of type Class A. Class B has all members of Class A, plus Class B specific things. Class B technically has all the private functions of Class A, but has no access to them outside of Class A’s scope due to the private keyword. But when Class B is within the scope of Class A, it gains access to private members within Class A.

This is my understanding of inheritance, scope, and access keywords. I do not know this for certain (never actually looked up the documentation surrounding this), but this is how I’ve come to understand it.

I welcome others’ input on the matter.

I would think it could get really confusing if you removed functionality from a derived class. That sounds like some fun troubleshooting.

Speaking of C#(as there is no public-private-protected inheritance like in c++),
I disagree. Child class has all the public and protected methods and fields of parent class. This is what I was told. But may be I do not understand how inheritance works.

As a whole your understanding seems to support the scenario.

This I agree that we should not write child specific thing on parents, because this would destroy the very purpose of inheritance. I messed up with parent-child relationship and now they are messing up with me :sunglasses::sunglasses:

In your example you’re accessing A_PrivateMethod from the context of ‘A’. The code running is a class A, so it knows about A_PrivateMethod.

Private doesn’t protect the object itself from accessing itself. It protects who can access it.

Private members can only be accessed from the scope of the owning class (blueprint). This means if you have 2 A’s, they can access each others private members. Also if you have a private static member, it can access private members of objects of the same type.

Basically… ignoring ‘split’ classes… if the private member was written in the same file as the accessing code, then they can access each other.

The fact it’s a B doesn’t matter… B’s are A’s.

2 Likes

I do believe it works the same way in C++ actually. Nested classes are considered friend classes and get access to private members. Child classes within the parent class will also have access to private members because they are of the same type. It’s the same as accessing private members as the parent class does.

Here’s an example, try to keep in mind that Public/Protected/Private are access modifiers. They define how classes can access members, it doesn’t prevent child classes from inheriting them:

public class A
{
    private void A_Private() { }

    public virtual void method()
    {
        B b_instance = new B();
        b_instance.A_Private(); // no error (inside A class, B is of type A,  B can access private A member)
        b_instance.B_Private(); // error (outside B class, cannot access private B member)

        A a_instance = b_instance; // no explicit cast necessary (same type)
        a_instance.A_Private(); // no error (inside A class,  A can access private A member)
        a_instance.B_Private(); // error (outside B class & A is not type B, A cannot access private B member)
    }
}

public class B : A
{
    private void B_Private() { }

    // B can override A's method, but A can't access this new method
    public override void method()
    {
        A a_instance = new A();
        a_instance.A_Private(); // error (outside A class, A cannot access private A)
        a_instance.B_Private(); // error (inside B class but A is not type B, A cannot access private B member)

        B b_instance = (B)a_instance; // explicit cast is necessary from A to B
        b_instance.A_Private(); // error (outside A class, B cannot access private A member)
        b_instance.B_Private(); // no error (inside B class, B can access private B member)
    }
}

As you can see, B is A, and contains all of A – but A does not contain members unique to B.

B contains new members that A does not know about. B contains all of A’s members, plus the new B members.

Inheritance is a 1 way street, but all the data is inherited.

1 Like

Ok lets summarize this, shall we?

A class has all of its members(fields and methods) plus all members(fields and methods) of parents class. Now having something does not let you access something. It depends upon the context(from where you are accessing).

**publics are accessible from everywhere.

**Protecteds are,

  1. accessible from the class where it was defined.
  2. accessible from the any class who’s parent can be traced back to the parent class, where those protecteds are defined. But protected are not accessible in upper class at inheritance chain. Meaning no parent can access protecteds of child but child can access parent’s protecteds.

**Privates are only accessible from the class, where privates are defined.

Lets get an example. Here is a particular inheritance chain:
A>B>C>D>E>F>G>H
A is the parent, B is the child. B is the parent for C, C is child…so on

Publics of E are accessible from anywhere. They are accessible from codes within A, B, C, D, E, F, G, H, main entrypoint function, library functions, within any other class who has nothing to do with the above chain.

Protecteds of E:
E is a kind of D, so E has everything of E and D. D is a kind of C, so D has everything of D and C.
So E has everything of E, D and C. Then C is a kind of B, so C has everything of C and B.
So E has everything of E, D, C and B. Then B is a kind of A, so B has everything of B and A.
So E has everything of E, D, C, B and A. Basically E has everything above its inheritance chain.

E’s protecteds are accessible from only class F, G and H. But E’s protected are not accessible from D, C, B and A classes. Because, D, C, B, and A, none of them are E. But E is D,C,B and A.

E’s privates are only accessible in E , via exactly E’s object or something which is like E’s

This is for public inheritance, haven’t looked at protected and private inheritance yet.

Plus one thing:
Speaking of C++, why there is public and protected inheritance?
What is the difference between
class B : public A{};

and this,

class B : protected A{};
??

Last time I observed, they both behaves exactly the same. There is a difference between public/protected and private though. If I do this:

class B : private A{}

then, all the protected members are accessible in B, but lets say C is also a derived class from B. Then inside C, I can not access protected members of A. Protecteds will not propagate further down to the inheritance chain.

Anyway, the public and protecteds are behaving the same, why I wonder!!??

1 Like

In C++

If we have BaseClass, ChildClass, and GrandchildClass. Where ChildClass inherits from BaseClass, and GrandchildClass inherits from ChildClass. There is also a StrangerClass unrelated to these types.

class ChildClass : public BaseClass
All scopes are aware ChildClass inherits from BaseClass, this includes StrangerClass.

class ChildClass : private BaseClass
Only ChildClass is aware ChildClass inherits from BaseClass… this includes GrandchildClass, if it were to inherit from ChildClass. GrandchildClass would not know that it is a BaseClass. StrangerClass knows nothing of ChildClass being a BaseClass.

class ChildClass : protected BaseClass
ChildClass and classes that inherit from it, like GrandchildClass, are aware they’re a BaseClass. But any other class is not aware, like StrangerClass, is unaware.

They aren’t… you need to be aware of the “Stranger” types.

Access modifiers don’t just pertain to the class inheritance chain. It pertains to all access, including access from types unrelated to the inheritance chain.

REMEMBER, Object-Oriented design isn’t just about inheritance. There’s 4 major principles:

Inheritance - a type can take on the functionality of an existing type

Encapsulation - a type can protect its members from direct access (public/private/protected)

Polymorphism - an object can be treated like another type if it’s related

Abstraction - data abstraction is the idea that through the use of the interface (methods of a type), the underlying data can be ignored, and there for abstracted. May you have a List that underneath is a Linked-List of items, or an array of items… they both act like Lists and have members like Lists… hence the IList type.

These all inter relate, so encapsulation (your access modifiers) don’t just deal with inheritance. They deal with it all.

A public member is how things act with your type.

A private members is things that are encapsulated away.

A protected members is a weird in between where you’ve abstracted data over a series of inherited types, but still need it encapsulated from other types.

Personally, I almost NEVER have a protected field. Only protected methods.

With that said, I use a limited amount of inheritance, and prefer a composition design through interfaces.

And always remember when discussing types, it’s best to think of them in the ‘is a’ relation.

ChildClass is a BaseClass.

This is what inheritance is saying…

Dog inherits from Mammal. Dog is a Mammal.

1 Like