Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
what the thing is I have two classes like (A,B)A:---Parent class and B:---Child class
1)If i create the object for child class i can access all the members of both A and B
now what is my thought is (2)why cant i access both the class members by creating object for
only parent class(Because both achieved parent and child relationship),Why cant parent access his child members..please suggest me for..../???
Posted

First of all all inherited/derived/child class can not access all members. They can access public/internal/protected members and not private members.

Second, actually when a class derived from another class actually a new version of original class is created. In that new version class some new features may added or override/hide some parent class members behavior.

When you can create parent/base class object, that time parent/base class does not know anything about its derived/child/new version class. For that reason parent class does not access its child class member.

Another reason, when you create child class you specifically mention its parent class. So child class know who is its parent class, but from parent class no way to specify who is its parent class. So summary is child class know its parent class so it can access its member and parent class does not know its child class so it can not access child class member.

Note: Using reflaction you can access child class property from its parent class. But it is not OOP concept. that you can call language/framework feature.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 24-Dec-13 23:29pm    
5
One of the major goals of OOP inheritance ... you can call it by various names, like: "modeling real-world objects and processes," "separation of concerns," "loose coupling," etc. ... is allowing a flexible structuring of what all objects share (what they inherit), and what any individual child object, or group of the same type child object implements, or defines, or overrides, separately from what they inherit.

.NET implements single-inheritance for Classes, and, through Interfaces, allows a very limited type of multiple-inheritance.

Within a Class that inherits, you can use the special operator "base" to access public fields and methods of the Class you inherit from. Note that "base," used as part of the Constructor Method definition (ctor) in a derived Class has a very different function.

Most of the time, imho, you'll be fine if you learn about basic Class inheritance, the use of Abstract Classes, and Interfaces, the use of the 'new and 'override keywords, and the special ancestor-accessor keyword "base" in derived Classes. And, you can round that out by learning how to use static classes, and, if absolutely necessary, static fields, or methods, inside dynamic classes.

I am not sure what follows is "the right thing," since it exposes you to some esoteric use-cases of inheritance in .NET, but I can't resist showing how, in fact, a base Class could keep track of its derived, or child, Classes. At the risk of taking you on a "dizzying" roller-coaster ride:
C#
using System;
using System.Collections.Generic;

namespace Dec25_InheritanceTest
{
    public class ParentClass
    {
        public int theInt = 500;

        public string theString = "some text";

        // this static generic List now becomes a Field of the Class:
        // one, and only one, instance of it exists
        public static List<ParentClass> theChildren = new List<ParentClass>();

        public void showValues()
        {
            Console.WriteLine();
            Console.WriteLine("in ParentClass");
            Console.WriteLine(theInt.ToString());
            Console.WriteLine(theString);

            Console.WriteLine("ParentClass has " + theChildren.Count.ToString() + " child classes:");

            // note how we can identify what Types the
            // derived Classes stored in ParentClass are
            foreach (ParentClass child in theChildren)
            {
               Console.WriteLine("\tClass: " + child.GetType().Name);
            }

            Console.WriteLine();
        }
    }

    public class ChildClass1 : ParentClass
    {
        // note use of 'new keyword
        public new int theInt;
        public new string theString;

        public int ChildClass1Int = 333;

        // in order to inherit from this Class
        // we must implement a parameterless
        // Constructor (ctor) even though
        // we never use it !
        public ChildClass1() {}

        // the ctor we use
        public ChildClass1(int theint, string thestring)
        {
            theInt = theInt;
            theString = thestring;

            // note we can this instance we are creating
            // in this ctor to the static List in the
            // ParentClass without Type conversion (casting)
            ParentClass.theChildren.Add(this);

            // here the use of 'base lets us access the
            // integer variable 'theInt defined in ParentClass
            base.theInt = 999;
        }

        public void showValues()
        {
            Console.WriteLine();
            Console.WriteLine("in child1 Class");
            Console.WriteLine(theInt.ToString());
            Console.WriteLine(theString);
            Console.Write("in ChildClass1Int = ");
            Console.WriteLine(ChildClass1Int);
            Console.WriteLine();
            base.showValues();
        }
    }

    public class ChildClass2 : ChildClass1
    {
        public new int theInt;
        public new string theString;

        // note the required use of the call to the parameterless
        // Constructor of the ChildClass2 this Class inherits from:
        // a colon sign following the end of the parameters followed
        // base() ... and this method will actually be called
        // when instances of this Class are created at runtime
        public ChildClass2(int theint, string thestring) : base()
        {
            theInt = theint;
            theString = thestring;

            ParentClass.theChildren.Add(this);

            //
            base.theInt = 777;

            // here using 'base lets us access the
            // integer variable defined in ChildClass1
            base.ChildClass1Int = 222;
        }

        public void showValues()
        {
            Console.WriteLine();
            Console.WriteLine("in child2 Class");
            Console.WriteLine(theInt.ToString());
            Console.WriteLine(theString);
            Console.Write("in ChildClass1Int = ");
            Console.WriteLine(base.ChildClass1Int);
            Console.WriteLine();
            base.showValues();
        }
    }
}
At this point, I invited you to execute this code in a C# Project where you have implemented the above Class definitions:
private void TestEsotericInheritance()
{
    ChildClass1 child1 = new ChildClass1(1,"child 1");
    child1.showValues();

    ChildClass2 child2 = new ChildClass2(2, "child 2");
    child2.showValues();
}
And put a break-point on each line, and single-step when you hit the break-point using <f11> and inspect the internal values as you watch what happens. Then examine the output in the Output Window of VS.

To me the most interesting aspect of this demonstration is that this code "works" without ever formally creating an instance of the ParentClass. You could also execute the line of code:
ChildClass2 child2 = new ChildClass2(2, "child 2");
by itself, and see that it also "works" without ever having created an instance of ChildClass1 !

So, I'd like to challenge you to infer what's happening in .NET when we can, apparently, get access to, and use, fields of Classes that are not static, and have no instances created. I'd call this a kind of "optical illusion" of .NET.

So, what's the point of this excursion into what's possible with inheritance in .NET ? imho: to be aware that such functionality is there ... if you require it ... and, to understand how the most typical uses of .NET's inheritance work, and that such typical uses may not require such functionality.

If you care to, please let me know if you think this type of example is useful, or: if it's just: distracting. I can live, half-comfortably, with the possibility that: while it may be valuable for me to regurgitate the digested remains of a several-year journey in C#, it may not be valuable to you :)
 
Share this answer
 
v4
C#
class parent
{ }
class child : parent
{ }


if you see the above example.

Parent class is inherited by child class.
so when you create an <code>instance for child class , the object can access all the public and protected members of the parent class.
because the child class if of type parent as well as child.

similarly when u create an instance for parent class ( parent class is no where related to child class , it is related from child class point of view only )
the object will access only the parent class public members.

hope you understood.
 
Share this answer
 
below is the layman explanation for ur question:

class Employee
{}
class Manager : Employee
{}

Manager inherits from employee.
So manager can perform all the actions(methods) that an employee object does. But manager has extra previleges(specific methods) which cant be called using a employee object.

If parent class can call child class methods, then there is no meaning for child(manager) class creation.
 
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