locked
Why a private member being inherited in child classes? RRS feed

  • Question

  • I have a program as follows:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestConsole_Oops
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program prg = new Program();
                prg .XYZ();
                B objB = new B();
                objB.XYZ();
                C objC = new C();
                objC.XYZ();
                Console.ReadLine();
            }
    
            private void XYZ()
            {
                Console.WriteLine("Employee");
            }
        }
    
        class B : Program
        {
            protected void XYZ()
            {
                Console.WriteLine("B");
            }
        }
    
        class C : B
        {
            protected void XYZ()
            {
                Console.WriteLine("C");
            }
        }
    }
    
    


    The function in class Program is marked as private, but still other base classes are able to call this function using there own object.

    How is it possible? Am i missing any OOPS concept here or something new in C#

    Friday, January 6, 2012 7:32 AM

Answers

  • Read first line of my post above. Private members are also inherited, but they can be accessed only in base class.

    Let me give you an example. A person 'A' has a landphone in his home. Let's consider landphone as a private property since it cannot be taken out of the house. Now, 'A' has a son 'B'. Now whenever 'B' is inside the home, he can always use the landphone, right? And whenever 'B' is outside the house he cannot use the landphone.

    Exactly the same concept applies to you example. Suppose XYZ is landphone of 'A'. And 'B' is son of 'A'. Since you have created object of B (objB) inside class 'A'  - which means son 'B' is inside the house and can use landophone - So, you can call 'XYZ' on objB !

    Also, Create an object of B outside the class A and try calling XYZ on it (similar to - Son goes outside the house and he cannot access landline which is inside the home)

    I hope this gives you an idea.

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, January 6, 2012 9:35 AM

  • Oh.. Sorry. I overlooked your question.

    Anyways. When you inherit a class, all members of the class including private members are inherited. The thing is, private members are not accessible outside of parent class. In your case, B & C are children of A. Also, you are creating objects of B & C inside class A. So, when you call XYZ method on object of B or C, compiler sees whether there exists any method named XYZ. If it exisits, it checks whether it can be called in the current context. Since, the current context is inside the class A itself, there is no problem. Just look at the IL of Main function. It calls XYZ function as if it calling it on own object.

    .method private hidebysig static void  Main(string[] args) cil managed
    {
         ...
         IL_0007:  callvirt   instance void GuiApplication.Program::XYZ()
         IL_000c:  newobj     instance void GuiApplication.B::.ctor()
         IL_0011:  stloc.1
         IL_0012:  ldloc.1
         IL_0013:  callvirt   instance void GuiApplication.Program::XYZ()
         IL_0018:  newobj     instance void GuiApplication.C::.ctor()
         IL_001d:  stloc.2
         IL_001e:  ldloc.2
         ...
    }

    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Rajasekhar.R Friday, January 6, 2012 8:14 AM
    • Marked as answer by Martin_Xie Monday, January 9, 2012 5:34 AM
    Friday, January 6, 2012 8:10 AM

All replies

  • Where is the private method is inherited here? In B & C classes you are defining new function XYZ.

    I think you have miunderstood inheritance concept. Just go through some simple tutorials on inheritance (two I have given below), which give you good idea of what is inheritance.

    http://msdn.microsoft.com/en-us/library/ms173149.aspx

    http://www.functionx.com/csharp1/examples/inheritance.htm


    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, January 6, 2012 7:40 AM
  • Adavesh,

    Thanks for the reply, but I think you misunderstood my question. refer to the modified code below:

     

    Class B and Class C are having function XYZ1 and XYZ2, and function XYZ is marked as "private" in class Program.

    Still the objects of class B and class C are able to call the function XYZ, I have the doubt here that is function XYZ is marked as private in class Program, B and C are the child classes, but still have the access to function XYZ (a private member of base class). My question is Why and How?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestConsole_Oops
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program emp = new Program();
                emp.XYZ();
                B objB = new B();
                objB.XYZ();
                C objC = new C();
                objC.XYZ();
                Console.ReadLine();
            }
    
            private void XYZ()
            {
                Console.WriteLine("Employee");
            }
        }
    
        class B : Program
        {
            protected void XYZ1()
            {
                Console.WriteLine("B");
            }
        }
    
        class C : B
        {
            protected void XYZ2()
            {
                Console.WriteLine("C");
            }
        }
    }
    
    



    -- A bug or a feature?
    Friday, January 6, 2012 7:49 AM
  • Hi,

    You have defined mehtod XYZ() in each class.

    You are creating instance of class and calling xyz() so each class picks its own copy of the method.

    Try inheritance with below code.

    class Program
    {
          static void Main(string[] args)
            {
                BaseClass baseClass = new SubClass();
    
                baseClass.method();
                baseClass.RandomMethod();
    
                SubClass subClass = (SubClass)baseClass;
                subClass.someMethod();
            }
    
            class BaseClass
            {
                public virtual void method()
                {
                    Console.WriteLine("BaseClass method");
                    Console.ReadLine();
                }
    
                public void RandomMethod()
                {
                    Console.WriteLine("Base Random Method");
                    Console.ReadLine();
                }
            }
    
            class SubClass : BaseClass
            {
                public override void method()
                {
                    Console.WriteLine("SubClass method");
                    Console.ReadLine();
                }
    
                public void someMethod()
                {
                    Console.WriteLine("test");
                    Console.ReadLine();
                }
            } 
    }
    
    Hope this helps!


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Friday, January 6, 2012 7:53 AM

  • Oh.. Sorry. I overlooked your question.

    Anyways. When you inherit a class, all members of the class including private members are inherited. The thing is, private members are not accessible outside of parent class. In your case, B & C are children of A. Also, you are creating objects of B & C inside class A. So, when you call XYZ method on object of B or C, compiler sees whether there exists any method named XYZ. If it exisits, it checks whether it can be called in the current context. Since, the current context is inside the class A itself, there is no problem. Just look at the IL of Main function. It calls XYZ function as if it calling it on own object.

    .method private hidebysig static void  Main(string[] args) cil managed
    {
         ...
         IL_0007:  callvirt   instance void GuiApplication.Program::XYZ()
         IL_000c:  newobj     instance void GuiApplication.B::.ctor()
         IL_0011:  stloc.1
         IL_0012:  ldloc.1
         IL_0013:  callvirt   instance void GuiApplication.Program::XYZ()
         IL_0018:  newobj     instance void GuiApplication.C::.ctor()
         IL_001d:  stloc.2
         IL_001e:  ldloc.2
         ...
    }

    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Rajasekhar.R Friday, January 6, 2012 8:14 AM
    • Marked as answer by Martin_Xie Monday, January 9, 2012 5:34 AM
    Friday, January 6, 2012 8:10 AM
  • Tiya,

    Thanks for investing your time, but my question is bit different. I'm seeking clarification on inheritance concept on private members, in my code, if you see the definition of XYZ (marked as private), this function is accessible by child class, conceptually this is not possible but is actually happening.


    -- A bug or a feature?
    • Proposed as answer by georgemathew Friday, January 6, 2012 12:04 PM
    • Unproposed as answer by georgemathew Friday, January 6, 2012 12:04 PM
    Friday, January 6, 2012 9:24 AM
  • Adavesh,

    Good clarification, thanks for the same. I have slight doubt here. If the current context is playing around here, isn't it the breach of OOPS concept? Whats your call on this?

    Because this is contradicting the basic concept of inheritance. I'm not fully convinced.


    -- A bug or a feature?
    Friday, January 6, 2012 9:29 AM
  • Read first line of my post above. Private members are also inherited, but they can be accessed only in base class.

    Let me give you an example. A person 'A' has a landphone in his home. Let's consider landphone as a private property since it cannot be taken out of the house. Now, 'A' has a son 'B'. Now whenever 'B' is inside the home, he can always use the landphone, right? And whenever 'B' is outside the house he cannot use the landphone.

    Exactly the same concept applies to you example. Suppose XYZ is landphone of 'A'. And 'B' is son of 'A'. Since you have created object of B (objB) inside class 'A'  - which means son 'B' is inside the house and can use landophone - So, you can call 'XYZ' on objB !

    Also, Create an object of B outside the class A and try calling XYZ on it (similar to - Son goes outside the house and he cannot access landline which is inside the home)

    I hope this gives you an idea.

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, January 6, 2012 9:35 AM
  • Only 2 words to say. Easy and Helpful. I think I need to brush up my OOPS skills.

    Thanks Adavesh.


    -- A bug or a feature?
    Friday, January 6, 2012 9:40 AM
  • Very nice explanation. Real world example always clear out the doubts.

    Hats off to you Avadesh :)

    Thanks!


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Friday, January 6, 2012 9:52 AM
  • Still the objects of class B and class C are able to call the function XYZ

    Actually, the "objects of class B and class C" are not calling XYZ, it's the Main method which calls XYZ. And Main is defined inside Program, and therefore able to call any private method of Program.

    • Proposed as answer by servy42 Friday, January 6, 2012 4:13 PM
    Friday, January 6, 2012 9:54 AM
  • really good example..
    George Mathew
    Friday, January 6, 2012 12:05 PM