C# Inheritance Member Access with Programming Example

In this chapter you will learn
  • Which type of member can be accessed by a child class?
  • Programming Examples and Codes

Which Type of Member can be accessed by a Child Class or Derived class?

A child class or derived class can access all the public, protected, internal and protected internal member. Private member cannot be accessed by child class however it is inherited and still present in child class and can be accessed using public property (GET SET modifier). There are two examples that demonstrate all the concept of member access clearly. First example will show which type of member can be accessed in child class and another example will show how to access private member in child class using GET SET modifier.

 

Programming Examples

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Member_Access
{
    class Program
    {
        static void Main(string[] args)
        {
            childclass child = new childclass();
            child.checkmember();            
            Console.ReadKey();
        }
    }
    class baseclass
    {
        public void public_member()
        {
            Console.WriteLine("I am Public Method");
        }
        protected void protected_member()
        {
            Console.WriteLine("I am Protected Method");
        }
        internal void internal_member()
        {
            Console.WriteLine("I am Internal Method");
        }
        protected internal void protected_internal_member()
        {
            Console.WriteLine("I am protected internal method");
        }
        private void private_member()
        {
            Console.WriteLine("I am private method");
        }
    }
    class childclass : baseclass
    {
        public void checkmember()
        {
            public_member();
            protected_member();            
            protected_internal_member();
            internal_member();
            //private_member(); //Raise Error. It can't be accessed
        }        
    }
}

Output
I am Public Method
I am Protected Method
I am protected internal method
I am Internal Method
_

If you uncomment private_member() then you will get compile time error
"'Member_Access.baseclass.private_member()' is inaccessible
due to its protection level"

Access Private Member in Child class

This example shows how you can access private member in a child class.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Member_Variable
{
    class Program
    {
        static void Main(string[] args)
        {
            childclass ch = new childclass();
            ch.check();
            Console.ReadKey();
        }
    }
    class baseclass
    {
        public int pub_var = 5;
        protected int pro_var = 6;        
        internal int inter_var= 7;
        private int pri_var = 8;
        public int Private_variable
        {
            get
            {
                return pri_var;
            }
            set
            {
                pri_var = value;
            }
        }
    }
    class childclass : baseclass
    {        
        public void check()
        {
            int sum = pub_var + pro_var + inter_var + Private_variable;
            Console.WriteLine("Total : " + sum.ToString());
        }
    }
}

Output
Total : 26
_

Summary

In this chapter you learned which type of base class member can be accessed in child class or derived class. You also learned to access base class private member in child class. The next tutorial will teach you Abstract and Virtual Methods in Inheritance.

More Articles